5

When catching a unhandled exception in a client application currently in user testing I made a code like this.

catch(Exception ex)
{
    var result = MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
    Current.Shutdown();
}

But the message box only appears very quickly and then the program shuts down. WHy doesn't the code wait for result to be apparent. How would I do that?

slavoo
  • 5,798
  • 64
  • 37
  • 39
Ingó Vals
  • 4,788
  • 14
  • 65
  • 113
  • That shouldn't happen. Use the debugger to find out what else is going on. – SLaks Aug 21 '12 at 17:38
  • 1
    use null in start of message box parameter, this will make message box a independent and top level interface. – greatmajestics Aug 21 '12 at 17:42
  • This is happening on startup, I guess that could be the problem. It's done before mainWindow is resolved. – Ingó Vals Aug 21 '12 at 18:00
  • We'll need more to go on than that. The code you posted (other than `Current.Shutdown()`, I don't know what `Current` is) works fine (message box shows before any window is created and doesn't exit before pressing OK). – Peter Ritchie Aug 21 '12 at 18:58

5 Answers5

6

I had a similar issue. Problem is that exception is thrown before the main window is displayed and thus there is no Window to display the message box on. Swamy's answer mere forced a time delay so that the window can be initialized. IMO not too elegant. Found a better solution [here]WPF MessageBox not waiting for result [WPF NotifyIcon].

Just specify the MessageBoxOptions.DefaultDesktopOnly in the MessageBox.Show(....) method.

Community
  • 1
  • 1
Renier
  • 480
  • 5
  • 13
3

From the documentation to MessageBox class:

Message boxes always have an owner window. By default, the owner of a message box is the window that is currently active in an application at the time that a message box is opened. However, you can specify another owner for the Window by using one of several Show overloads. For more information about owner windows, see Window.Owner.

Thus your problem is really similar to this question where the application exists before message box is dismissed by the user.

The first thing to try is to pass null as Window parameter.

MessageBox.Show(null, ex.Message, "Error", MessageBoxButton.OK);

If you have an instance of Window class, for example your main window object, pass it instead of null:

MessageBox.Show(mainWindow, ex.Message, "Error", MessageBoxButton.OK);

If you are currently in any of the main window methods, you can pass this as the Window:

MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK);

Use null only in case where there's no main window object available at this time.

Look at MessageBox sample. You should be able to see the difference between the cases where this is passed as the owner window and where null is passed.

Community
  • 1
  • 1
Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
  • OK null only throws a null reference exception but I was wondering if I the owning window currently hasn't had the Show() methods on it call is it still good as a proper owner? – Ingó Vals Aug 22 '12 at 16:50
  • Yes, the owner window can be not visible on the screen. – Alexey Ivanov Aug 23 '12 at 10:08
  • When using `null` it may be ambiguous which function you want to call. To make it explicit, you can cast `null` to `Window` or create a local variable `Window owner = null` and use `owner` variable as the parameter to `Show`. – Alexey Ivanov Aug 24 '12 at 20:49
2

Are you using this code inside the script or actual .net application? You are missing "s" in message box button. you should writte code like this:

try
{
    throw new Exception("My new exception");
}
catch (Exception ex)
{
    DialogResult result = MessageBox.Show(ex.Message, "Error",MessageBoxButtons.OK);
} 

If this will not work you should look at the code that throws your exception for further debugging...

slavoo
  • 5,798
  • 64
  • 37
  • 39
Gregor Primar
  • 6,759
  • 2
  • 33
  • 46
  • I guess your code is from Windows Forms because MessageBoxButton is correct and the result is MessageBoxResult. Tried that though but didn't change anything. I think this has to do with me instancing this messagebox before the mainwindow is created. – Ingó Vals Aug 21 '12 at 18:01
  • It's nice to have two different `MessageBox` classes doing roughly the same: `System.Windows.Forms.MessageBox` and `System.Windows.MessageBox`. I consider it quite confusing, in particular when asking questions. Though the question is tagged with `wpf` which implies `System.Windows.MessageBox` is used. – Alexey Ivanov Aug 21 '12 at 19:58
0

This is very old question, but probably below answer may hep some one else,

I had the same situation where I wanted to show the Message box before the Main window is created, and in My case too , the message Box was disappearing Before I click on OK and application would shut down.

I am using async and awaits and adding a delay as low as 10 millisecond fixed the problem for me.(As the main window is loaded by that time)

await Task.Delay(10);
if (MessageBox.Show(MainWindow, "Message", "Error", MessageBoxButton.OK) == MessageBoxResult.OK)
{
    Current.Shutdown();
}

Probably for non Async methods Thread.Sleep could also work (not tried).

Swamy
  • 193
  • 1
  • 7
0

Before any Window is shown in a not async Methode, this solved the Problem for me:

 if (Task.Run(() => MessageBox.Show("test", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error)).Result == MessageBoxResult.OK)
 {
     Environment.Exit(1);
 }
Michael Santos
  • 466
  • 7
  • 15