There are arguably few greater frustrations for end users than programs that crash or hang, especially if the problem causes them to lose work. Microsoft has invested considerable engineering efforts in proprietary applications such as Microsoft Office to ensure that they save data to temporary files periodically, so users’ data can be recovered if the application hangs, crashes, or is forcefully closed by an operating system reboot.
Beginning with Windows Vista, developers can enhance the recovery and restart functionality of their own applications by registering their applications with the Application Recovery and Restart (ARR) APIs. These APIs receive operating system notification prior to an unexpected application exit, allowing them to save a copy of the user’s input, which can then be retrieved the next time the application runs. An application can also register with the operating system for automatic restart using ARR functionality, which results in the application being restarted if it crashes after successfully running for 60 seconds. (This minimum application lifetime prevents applications that crash constantly on start-up from entering an infinite restart cycle.) While you can use the recovery and restart support in ARR independently, it’s generally advisable to take advantage of both feature sets simultaneously.
Registering for application restart is a simple matter of calling the Windows API function RegisterApplicationRestart. The function takes two parameters-the command line to pass to the application if a restart is required, and a flag value that determines whether the application is restarted when it crashes, hangs, crashes during a patch process, or when the operating system restarts as a result of an update. Passing zero (0) as the flag value means that the application will restart in all scenarios (provided it has been running successfully for 60 seconds).
The most convenient place to call RegisterApplicationRestart is usually in the entry point of the application, because that provides the best assurance that the API call will not be missed, and it’s an easy way to ensure that the original command line will be readily available if required. Applications can unregister for restart by calling UnregisterApplicationRestart.
When an exception occurs, users will see a standard Windows error message box (see Figure 1).
Figure 1. Initial Crash Message: When an application crashes or hangs, users see a standard error dialog.
For restart-registered applications, users will also see a second message box (see Figure 2), and the application will then restart.
Figure 2. Application Restart Message: When an application has been registered for restart, users will see this second message, informing them that the application is restarting.