Build Build Resilient Applications with Windows Recovery and Restart Applications
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.
More for Developers
On the Codeguru Forums
Visit the Forums »eBooks, Webcasts, Whitepapers and More
Tutorial: Light Up Your Windows 7 App with Jump Lists, Libraries, and Microsoft Platform Ready
Custom Walkthrough: Create an Azure-integrated Windows 7 Phone App
Article: Thuzi, Outback and Azure--Tapping the Power of Facebook and the Cloud
Article: Windows 7 Light-Up--Make Your Applications Shine on Windows 7
Article: Submitting Your Windows Phone 7 Application to the Windows Marketplace



Managing a mixed IT infrastructure is always a challenge. The challenge is compounded for mid-sized companies where you don't have the staff resources of larger firms. A variety of systems and equipment creates a complex infrastructure where finding and solving performance and network glitches is really difficult. Learn how Hitachi IT Operations Analyzer can help.