Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating A MessageBox That Doesn't Stop Code?

Tags:

c#

messagebox

Ok, I'm looking for something pretty simple: creating a MessageBox that doesn't stop my code.

I'm guessing I'll have to create a different thread or something? Please advise on the best way to accomplish this.

Thanks!

like image 885
sooprise Avatar asked Jul 08 '10 16:07

sooprise


People also ask

Which of the following is default button of MessageBox control?

MessageBox with Default Button By default, the first button is the default button. The MessageBoxDefaultButton enumeration is used for this purpose and it has the following three values. The following code snippet creates a MessageBox with a title, buttons, and an icon and sets the second button as a default button.

How do you use message box?

Access to the message box is provided by the MessageBox class. A message box is displayed modally. And the code that displays the message box is paused until the user closes the message box either with the close button or a response button.

How do I change the MessageBox name in VBA?

You can change the title of the message box by changing "Title name" in the VBA code. Message: In this example the message that will appear in the message box is "Excel is great". You can change the message that is displayed in the message box by changing "Excel is great" in the VBA code.


2 Answers

You could spin up another message pump by calling it on separate thread. MessageBox.Show pumps message so it is safe to do without a call to Application.Run.

public void ShowMessageBox()
{
  var thread = new Thread(
    () =>
    {
      MessageBox.Show(...);
    });
  thread.Start();
}

Edit:

I probably should mention that I do not recommend doing this. It can cause other problems. For example, if you have two threads pumping messages then it is possible for the message box to get stuck behind another form with no way to make it go away if the form is waiting for someone to close the message box. You really should try to figure out another way around the problem.

like image 172
Brian Gideon Avatar answered Oct 18 '22 17:10

Brian Gideon


No, You're going to have to make your own message box form. the MessageBox class only supports behavior similar to .ShowDialog() which is a modal operation.

Just create a new form that takes parameters and use those to build up a styled message box to your liking.


Update 2014-07-31

In the spirit of maintaining clarity for anyone else who finds this through google I'd like to take a second to explain this a bit more:

Under the hood MessageBox is a fancy C# Wrapper around the Windows SDK user32.dll MessageBox Function and thus behaves exactly the same way (after converting .NET Enums into the integers that represent the same thing in the system call.

What this means is that when you call MessageBox.Show() the call is marshaled out to the OS and will block the current thread until an option is selected or the window is killed. To prevent your code from being halted you need to launch the message box on a seperate thread, but this will mean that any result that comes back from the message box (Yes / No / Ok / Cancel / Etc...) will be returned to the separate thread that was tasked to call the message box.

If you act on the result of this message box launched this way you'll have to Dispatch the result back to the UI Thread for Thread Saftey.

Alternatively you can create your own message box form in WinForms / WPF and call it with the .Show() method. Any click events on the buttons will execute on the UI Thread and you will not have to dispatch the calls back to the UI Thread to manipulate things in the UI.

like image 28
Aren Avatar answered Oct 18 '22 16:10

Aren