views:

65

answers:

2

If my application crashes, I intercept the crash (using the function SetUnhandledExceptionFilter). In my crash handler, I create a mini dump file, and notify the user that his application has crashed. This notification is done via a MessageBox with the flag MB_TASKMODAL so the rest of the application is blocked.

Unfortunately, that doesn't block the handling of repaint- and timer-messages. Especially the timer messagesare very annoying since they may execute all kinds of intermediate actions (depending on which plug-ins are loaded in my application), even saves to backup files, etc.

Is there an easy way to prevent Windows from sending repaint- and timer-messages(while showing the "you have crashed" popup)?

An alternative would be to use some global variable, that would be set in my crash handler, and checked in every place in my application where I would execute some logic, but this seems a rather 'dirty' and non-flexible solution to me. Isn't there an easier way? (and only making sure that the message loop of the message box only handles message box messages and no messages of other windows).

Thanx Patrick

+1  A: 
  • it's not possible to prevent Windows from sending repaint and timer messages. After all, you do want you message box itself to be repainted as well.

  • a modal message box blocks the execution of the UI thread it was shown on. This still leaves background threads you spawned or work items you scheduled on the thread pool running.

  • you've taken the mini-dump and you know your app has crashed. Why do you want to keep the process around and show the message box from inside it? You obviously don't expect the app to be able to recover, so the reasonable thing at that point is to kick off a small helper process that would show the message to the user and terminate the crashing application.

Franci Penov
Two reasons why you would want to show the "we are sorry" popup:- politeness- being able to hook up a debugger at the moment of the crash (if crash is local, or using remote debugging)Also, I want this behavior to be within the application, not relying on other exe's that must be installed on the computer.
Patrick
I agree that showing the popup is a good thing to do. However, a message box is pumping messages, and causing the app to continue running. What you want is to "freeze" the app at the moment of crash. Btw, if you want to enable hooking a debugger, you might want to look into using the Watson APIs which will enable JIT debugging.
Franci Penov
Franci, can you point me to a place where I can find more information about the Watson API? I don't find anything useful on MSDN and in Google I only find the API for a product that performs Semantic-Web searches.
Patrick
The official name for Watson is "postmortem debugger". It's a way to specify a process that will be automatically launched when an application crashes. Here's an overview of how this works - http://blogs.msdn.com/b/junfeng/archive/2007/11/19/from-unhandled-exception-to-debugger-attach.aspx .
Franci Penov
Note that official Microsoft recommendation is to leave the default postmortem debugger and enroll in the WinQual program to get access to crashes specific to your application. However, you can certainly create your own process to ask users to run as postmortem debugger, when reproing crashes. Of course, registering it would require administrative privileges, and this is not something I'd recommend you do automatically on each install.
Franci Penov
The best approach would be to have a diagnostic config tool for your app, which registers temporarily your postmortem debugger and also sets a registry key to reconfigure your app in advanced diagnostics mode, where your does aggressive preconditions checks at critical places and call DebugBreak. Such combination would allow you to collect minidumps with the process state before something bad has happened, but you have suspicions it is about to happen.
Franci Penov
A: 

There is only one way to show a crash dialog safely - on a seperate thread created specifically for it. Creating a dialog on a crashed thread will cause all sorts of side effects (as you have noticed) as it will pump and dispatch both posted and sent messages destined for windows on that thread.

The question is - is it safe to create a dialog from within a crash handler? I can think of several scenarios where it could be problematic - especially if the global loader lock has been involved (i.e. a crash happens somewhere inside object initialization caused by a dll being loaded).

So its probably a good idea to create the thread on app startup and have it on standby.

Chris Becke
Good idea. Showing the message box in a separate thread is something that can be easily done (I'm not a Windows expert).
Patrick
Yep, this works.In theory I should also suspend all other threads while the popup is being shown, but that will be something for later.Thanks for the answer.
Patrick
I just want to note (again) that showing a message on one thread is not a guarantee that other threads will not continue running.
Franci Penov