views:

165

answers:

2

To pop-up a message box, I'm using MessageBox.Show(...). I usually wrap the call in an Invoke:

BeginInvoke (new Action (() => {
    MessageBox.Show ());
}));

(I removed a part of the original question which was answered elsewhere)

Do I always need to wrap the MessageBox call in an (Begin-)Invoke if I'm calling from a non-GUI thread?

A: 
  1. As far as I know, as long as you don´t supply the owner parameter in the call to MessageBox.Show you don´t have to invoke the call on the UI thread.
    That is, if you supply the owner parameter you have to invoke the call to MessageBox.Show on the tread the owner was created on.
    -- Please correct me if I´m wrong --
  2. That depends on if you want to have the UI thread continue execution or if you want it blocked while showing the msgbox.
Jens Granlund
+1  A: 

Short answer: yes, because I would consider it a best practice.

Longer answer:

You should not get into a situation where you have to ask yourself this question, at least not in the long run. Usually, in a well designed piece of software, you have defined "gateways" between the user interface and the "rest of the world", this would also be the place where you raise the events that notify the GUI that something has to be done (via BeginInvoke(EventRaiserMethod(params))).

stormianrootsolver
From a design point of view, that sounds very reasonable and I agree. However, I'm also interested in the actual answers.
mafutrct
Sorry to have no better answers, hm... but I'd say, that your first question (about the necessity) is mostly a question of style and interpretation. Question is: do you want to put the question of thread safety into Microsofts hands? Do you know if they actually have that "Invoke" - wrapper pattern in their "MessageBox.Show(...)" - method? What if they do and change that later? Regarding the second question: I'm a bit astonished that it makes no difference. Will try that later myself, this looks very interesting.
stormianrootsolver
About 2): MessageBoxes have their own message pump, I believe that's responsible.
mafutrct
Turns out I confused blocking calls to the GUI with a blocking call on the GUI. Part 2) is solved. ;)
mafutrct