Search code examples
.netinvokemessagebox

Do I need to invoke MessageBox calls?


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?


Solution

  • 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))).