Cocoa context menu causing crash when timer on main thread - cocoa

I am working in mono on OSX10.6 - but this question is for Cocoa experts also please.
I have a context menu which I attach to an NSTableView. When I select the context menu the handler (which is empty for test) is called ok but then the application crashes.
This is the code I have in the window controller:
...
myNSTableView.Menu = new NSMenu();
NSMenuItem mi = new NSMenuItem("menu item 1", myHandler);
myNSTableView.Menu.AddItem(mi);
...
public void myHandler(object Sender, EventArgs e)
{
}
I also have an NSTimer setup to repeat every 200ms
myNSTimer = NSTimer.CreateRepeatingScheduledTimer(new TimeSpan(2000000),delegate {
move an NSSlider
});
NSRunLoop.Main.AddTimer(myNSTimer, NSRunLoopMode.Common);
NSRunLoop.Main.AddTimer(myNSTimer, NSRunLoopMode.EventTracking);
As soon as I try to access the context menu, the application crashes - every time.
If I dont start the timer, the context menu is fine.
I have tried literally everything to get this to work and have run out of ideas, so I really need someones help please, even just a pointer to where the problem might be. Forgetting about the fact that its mono just now, can anyone see from a cocoa point of view a problem?

It turned out that the problem was exacerbated by the timer, but the cause was actually that I wasn't declaring instance variables for the NSMenuItems, so they were getting garbage collected and causing crash at objc level.

Related

DelayedConfirmationView doesn`t while changing layouts

I am pretty new to programming and trying to build my first own app for my Android Wear.
Basically I want to see a new layout when pressing one of two buttons. On the new layout should be a DelayedConfirmationView with the possibility to cancel and to go back to the previous layout (kind of using "ok google" "Set timer to 5 seconds")
For this I created two layouts and coded in the manifest:
public void onClickcallSpringer(View view) {
setContentView(R.layout.callspringer);
DelayedConfirmationView DelayConfirmationView = (DelayedConfirmationView) findViewById(R.id.delayed_confirmation);
DelayConfirmationView.setTotalTimeMs(2000);
DelayConfirmationView.setListener((DelayedConfirmationView.DelayedConfirmationListener) this);
DelayConfirmationView.start();
}
This leads to an crash, when pressing the button in my app.
I tried to understand the function DelayedConfirmationView from another App, but there it also leads to a crash (https://github.com/livefront/android-wear-demo).
I hope somebody can help me with this (and maybe explain to me how to solve this with a new activity as well).
Found my mistake: I had to make a new activity.
I am using now a .setOnClickListener to start a new Activity
button_callspringer.setOnClickListener((v) -> {
startActivity(new Intent(MainActivity.this, callspringer.class));})
and in the new class I coded on the onCreate-Methode:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.callspringer);
DelayedConfirmationView delayedconfirmationview_experte = (DelayedConfirmationView) findViewById(R.id.delayed_confirmation_springer_view);
delayedconfirmationview_experte.setTotalTimeMs(5000);
delayedconfirmationview_experte.setListener(this);
delayedconfirmationview_experte.start();}
This leads to the wished result. Maybe this will help someone one day too.

wxWidgets event focus textcontrol

I have another wxWidgets question regarding events and focus.
I have already looked at the tutorials and this old question here but I am still running into problems C++ Event (Focus) Handling
Basically I have a dialog with two wxTextCtrl elements and a Button.
What I would like to achieve is, that when I click on button it needs to tell me which of the two elements previously had the focus.
In the constructor of my Dialog I created all the elements and then connected them to the eventhandler like this: Ttop->Connect(TOP,wxEVT_KILL_FOCUS,(wxObjectEventFunction)&UI_ADDENTRY::hasfocus);
Tbottom->Connect(BOTTOM,wxEVT_KILL_FOCUS,(wxObjectEventFunction)&UI_ADDENTRY::hasfocus);
then there is the eventhandler that safes the id into focus
void UI_ADDENTRY::hasfocus(wxFocusEvent& event){
focus= event.GetId();
event.Skip();}
however when i try to access focus in the Button function it always tells me: 0 instead of TOP or BOTTOM / the ids that I gave the textcontrols
void UI_ADDENTRY::OnRecord(wxCommandEvent &event){
wxString tmp;
tmp << this->focus;
wxMessageBox(tmp);}
What am I doing wrong? is there another way of finding out which of the two textbox has been in focus last?
Thank you
The most fool proof way is to catch EVT_SET_FOCUS in your text controls and remember the last one that received it. This is not more difficult than what you are doing but should work without problems.
FWIW EVT_KILL_FOCUS can't, unfortunately, be consistently implemented on all platforms, in particular GTK+ doesn't give any information about the window focus is being lost to.
In think u mean event.GetWindow().GetId(). Though I'm not sure how ur casting from int to string.

Mono navigation

I just started out with mono and I've already gotten problems. I'm used to play with c# code and was told that mono would be easy for me, but no no. I simply want to start a new activity and close the one i just were using. I checked out some mono API examples, but they are simply too complicated for this task. It has to be some easier way of doing it. This is my first activity class:
[Activity(Label = "CryptotoDroid", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity
{
EditText inputpassword;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
inputpassword = FindViewById<EditText>(Resource.Id.beforetext);
button.Click += delegate
{
if (inputpassword.Text == "Moo")
{
StartActivity(typeof(ActivityContacts));
}
};
}
}
This is what i tried, but the program crashes. I Simply want to make the program to start a new activity when the password is "moo".
The activity I want to start is:
[Activity(Label = "My Activity")]
public class ActivityContacts : Activity
{
protected override void OnCreate(Bundle bundle)
{
SetContentView(Resource.Layout.Main);
var contactgrid = FindViewById<GridView>(Resource.Id.gridview);
}
}
Later on, i would also like to fill out my gridview with all contacts in the phone, but that belongs to another topic.
Did you mean to set both activities' content views to the Main layout?
SetContentView(Resource.Layout.Main);
public void setContentView (View view)
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.
For starting with MonoDroid, I recommend:
getting hold of one of the excellent books out there (Wally, Greg, Chris or others will be along with suggestions - I've personally yet to see a bad one so won't give a recommendation!)
try watching a couple of the Xaminar's on youTube -http://www.yourepeat.com/g/Xaminar
try building the Xamarin sample programs and then try adapting them - it's sometimes easier to adjust working programs than it is to start new ones (sometimes!)
try building new things just as you have above.
When you hit problems - as we all do - then please do ask questions here or on the Xamarin forums - people will help.
However, when you hit a crash or exception, please try:
Give us as much info as you can about the crash/exception - including there are some ways to get extra debug logs off of a phone or emulator - http://docs.xamarin.com/Android/Guides/Deployment,_Testing,_and_Metrics/Android_Debug_Log - these logs often contain important text which will help us help you diagnose the crash
If you are running under the VS2010, VS2012 or MonoDevelop debugging tools, try adding extra Console.WriteLine statements and/or use breakpoints - this can help you and us work out which line is causing the crash - or if the crash is occurring somewhere in setup before the code even runs.
Speaking personally, I believe Mono for Android does help experience C# devs exploit their skills on Android - but there's still new things to learn, and there are still embedded development frustrations to tackle and overcome (like these sort of crashes)

Visual Studio C++ How to get the Form not freezing while calling a time-consuming function?

I am making a C++/CLI Forms application.
In the main window of my app I have a button. When I click that button I call the Load function. Below there is the C++/CLI code:
private: System::Void Button1_Click(System::Object^ sender, System::EventArgs^ e) {
Load();
}
The function Load() is a time-consuming function. It uses the cURL library to send several HTTP GET request to a website.
In the Form I also included a ProgressBar and a textLabel showing the current request being sended.
The problem is that when I click the button and call the function the Form just freezes. I can't see the progressBar and Textlabel changing it's value while the function Load() is called, the Form is just freezed. When the function Load() has finished sending request, suddenly the progressBar change It's value to 100%.
I hope I described my problem clearly enough to understand it.
Move your task to another thread, or call Application.DoEvents();, just after you updating your scrollbar value.
Either break the task into smaller parts (design a finite state machine or use continuations) or use a separate thread.
The first approach takes more getting used to, but it's easier for an experienced programmer to get right. Threading requires synchronization which is very detail-oriented and causes a lot of hidden sporadic bugs which are extremely difficult to debug.
Call Form1.Refresh() every time you update an element of the form (say Form1). It will show the results immediately.
Before any line command that make probably any load time ...Write This:
System::Windows::Forms::Application::DoEvents();

RBSplitView has delayed reload of autosaved view positions

I really enjoy using RBSplitView, an open source replacement for NSSplitView, but I have a problem in my shipping app and am experiencing it again in a new project.
The problem is I'm telling the RBSplitView to autosave its position state by giving it an autosave name. When my app launches the RBSplitView doesn't seem to honor the saved state till a second after the window is drawn.
I've spent the night trying to debug the behavior but have had little success. Anyone out there use this lib and have some advice?
You can scrub this quicktime movie to the issue at work:
http://media.clickablebliss.com/billable/interface_experiments/rbsplitview_delayed_autosave_reload2.mov
I've still been unable to figure out why this is happening but I do have a workaround.
First, make sure your main window is not visible at launch and then at the end of applicationDidFinishLaunching in your app delegate add something like:
[mainWindow performSelector:#selector(makeKeyAndOrderFront:) withObject:self afterDelay: 0.1];
The delay is the key. If you just tell the window to makeKeyAndOrderFront: I still see the issue. However as long as it has a beat of time it looks good.
This likely is happening because the RBSplitView instance needs to wait until it's first moment to get to set its frame to the autosaved value, which happens to be after the user can see it. This 0.0-delay trick simply delays showing the window until the very next runloop, which gives the split view a chance to do its magic (and other views) so that when the user sees the window, it's already nice and sexy. So just do the delay at 0.0 and you'll be fine.
I have a similar, but slightly different workaround in my app that uses RBSplitView. In applicationDidFinishLaunching:, I call adjustSubviews on the split view before calling makeKeyAndOrderFront: on the window that contains it. This seems to knock the split view in to order before it gets displayed on the screen.

Resources