views:

1182

answers:

4

Hi,

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 poping up. I had the feeling in .net framework 2.0+ you dont need to marshall this particular call, it does it for you. Correct?

regards ewart.

this is a semi-related topic for interest: http://stackoverflow.com/questions/195593/why-use-a-owner-window-in-messagebox-show

+3  A: 

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.

Marc Gravell
A: 

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.

Scott Langham
A: 

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:

public DialogResult ShowMessageBox (String message, String caption)
{
    if (this.InvokeRequired) {
     return (DialogResult) this.Invoke (new PassStringStringReturnDialogResultDelegate (ShowMessageBox), message, caption);
    }
    return MessageBox.Show (this, message, caption);
}

public delegate DialogResult PassStringStringReturnDialogResultDelegate (String s1, String s2);

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.

ZaijiaN
+3  A: 

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.

JaredPar