If my application crashes, I use an ExceptionFilter to catch the crash, perform some final actions, then show a message box to the user that the application has crashed.

Because the application already crashed, there's not much I can (or I dare) to do, because if I do too much, the executed code might access corrupted memory and crash again. Some of the things I currently can't do (or I don't dare to do) is to close network connections, Oracle database sessions, ...

Problem is that if an application crashes, and the user is out to lunch while the MessageBox is open, other users might be blocked, because of the open database session. Therefore I want:

  • Either a MessageBox with a time-out. Problem is that you can't do this with the standard MessageBox Win32 API function, and I don't want to make a specific dialog for it (because I want to minimize the executed logic after the crash)
  • Or the possibility to close the MessageBox from another thread (the other thread can provide the time-out logic).

Did I overlook something in the Win32 API and is there a possibility to have a MessageBox with a time-out?

Or what is the correct way to close an open MessageBox from another thread (how to get the MessageBox handle, how to close it, ...)?

+1  A: 

This does not justify a thread.

The best solution would be to use a modal dialog box that registers a timer for auto-close.

Pavel Radzivilovsky
I wanted write the same suggestion: create a new dialog with respect of `DialogBox`, create a timer with `SetTimer` inside of `WM_INITDIALOG` and close the dialog in `WM_TIMER`.
+3  A: 

You should ask yourself, why you want a messagebox in the first place. When it is OK that the message box is not seen when noone is sitting in front of the computer, why isn't it OK that the user doesn't see a message when his program disappears?

If you really want it, I think the simplest solution is to spawn a new process displaying the message. It can run as long as it wants and does not interfer with your crashing program.

+1  A: 

What about just logging the event to a local file (and record memory dumps or whatever information you might need for later debugging)?

You could close your application, close network connections and do your housekeeping stuff.

As soon as the application is started again, you can inform your user (based on local file information) that the application has crashed during last execution.


I would run your original code from inside a wrapper application that does a CreateProcess and then a MsgWaitForMultipleObjects on the process handle (most process launching code samples use WaitForSingleObject, but you need to guard against messaging deadlock scenarios). Your watching process can then detect the failure of the spawned process, pop up its own timed-out dialog, and exit on user response or timeout.

I think that's the cleanest solution which prevents your unstable program having to execute any code.

Bob Moore

I noticed that if the main thread simply exits the application while the other thread still has the ::MessageBox open, that the MessageBox is being adopted by a process called CSRSS. This solves my problem, since this only requires a time-out on the Event in the main thread (WaitForSingleObject with timeout).

However, this raised another question:

+1  A: 

While I agree that spawning a new process to display a fire-and-forget dialog is probably best, FWIW there is actually a timeoutable messagebox function exported from user32 on XP & above; MessageBoxTimeout (as used by things like WShell.Popup())

Alex K.

A Win32 MessageBox really is a dialog, with a dialog message pump. You can therefore rely on standard Win32 timer messages (WM_TIMER). Send one to your own window, and when you do get it, dismiss the MessageBox by sending a WM_COMMAND/BN_CLICKED message to the ID_OK button.

The messagebox, since it's a dialog, will be class "#32770". Since it's the only dialog box you will have open, it's easy to find amongst your child windows.