I launch a thread via ThreadPool.QueueUserWorkItem which has a messagebox dialogue in it:
System.Windows.Forms.DialogResult dr = System.Windows.Forms.MessageBox.Show("would you like to download upgrade in background? ..", "Upgrade Available", MessageBoxButtons.YesNo);
It seems to work fine however I am a little suspicious after some customers suggested they were not getting the message popping up. I had the feeling in .NET framework 2.0+ you do not need to marshal this particular call, it does it for you. Correct?
This is a semi-related topic for interest: Why use a owner window in MessageBox.Show?
No, it doesn't Marshal to the UI thread. If you think about it, it wouldn't be possible for it to do so.
It's possible to have more than one UI thread in an application. Some programs, such as internet explorer, have many UI threads. Which UI thread would the .Show call pick?
It's also possible to use MessageBox.Show in an application that has no UI threads. You can very well call this in a Console application and it will work.
MessageBox.Show will show UI on the thread it is called from. If there isn't already a message pump running on the thread it will setup a temporary one in order to function. It will tear it down after the Show call completes.
As a general rule, you shouldn't do GUI work outside of the main/application thread. I'd make a ShowMessageBox method on the parent form that can do an Invoke:
BUT ALSO KEEP IN MIND: when debugging a multi-threaded GUI app, and you're debugging in a thread other than the main/application thread, YOU NEED TO TURN OFF the "Enable property evaluation and other implicit function calls" option, or else VS will automatically fetch the values of local/global GUI objects FROM THE CURRENT THREAD, which will cause your application to crash/fail in strange ways. Go to Tools->Options->Debugging to turn that setting off.
Sorry for the caps, but this took me DAYS to figure out why I every time I tried debugging my app it would hang and then crash.
Well, I would marshal and specify a window, if only so the
MessageBox
can get the correct focus. It might be they simply can't see it because it is behind one of your forms, and doesn't know it should be in the foreground.Is this an application or a service. If it's a service, maybe it's not set up with the 'Allow interaction with desktop' permission.
See the properties of the service in the services control panel applet.