Registering for Application Restart
Getting Windows to restart your application automatically after a failure follows a process similar to the recovery code you've already seen. First, you register the application for a restart in the event of an unexpected failure using the following code:
// RegisterApplicationRestart as defined
// by Application Recovery and Restart
public static extern Int32 RegisterApplicationRestart(
String commandLineArgs,
UInt32 flags);
// Registering for restart in your application
string commandLineArgs = "/recovered";
Int32 result = ApplicationRecoveryRestart.
RegisterApplicationRestart(commandLineArgs, 0);
The
RegisterApplicationRestart method accepts two parameters. The first parameter is a string containing the command-line arguments Windows will pass to your application when it restarts.
Do not include the executable in this parameter; Windows already knows what it is. You may pass in
null if you don't need to provide command-line arguments. The second parameter is reserved for future use; pass in zero.
Take careful note of these two caveats before you move forward with testing and deploying an application registered for restart, because Windows will restart an application only:
- After it has been running for 60 seconds or more. This is a safeguard against cyclical restarts—repeatedly restarting an application that fails on startup or shortly after.
- When WER detects an unexpected termination or hang. See the Restart Manager API for information on how to restart applications during installation or update. Restarting an application after it has failed may warrant taking extra steps during startup and initialization. Use the command-line arguments parameter in RegisterApplicationRestart to signal this condition.
| Author's Note: As of this writing, the MSDN documentation for the RegisterApplicationRestart method incorrectly states that Windows displays a confirmation dialog box before restarting an application. Happily, this isn't the case; your server applications will restart automatically when they're unattended. |
 | |
| Figure 4. Application Restart: After a failure, the sample application recovers and restarts in a new window. |
The sample application notifies you when it has been running for 60 seconds. The background window in
Figure 4 shows an application that ran for 60 seconds and then failed. The application in the foreground is the new instance that Windows launched automatically.
Also much like the recovery code, you can retrieve your application restart settings with a call to
GetApplicationRestartSettings or reset them with
UnregisterApplicationRestart.
// GetApplicationRestartSettings as defined
// by Application Recovery and Restart
public static extern Int32
GetApplicationRestartSettings(
IntPtr process,
StringBuilder commandLine,
ref UInt32 size,
out UInt32 flags);
// Retrieving application restart settings in your application
UInt32 capacity = 256;
UInt32 flags;
StringBuilder commandLineArgs = new StringBuilder((int)capacity);
ApplicationRecoveryRestart.GetApplicationRestartSettings(
Process.GetCurrentProcess().Handle,
commandLineArgs,
ref capacity,
out flags);
// Resetting application restart settings
ApplicationRecoveryRestart.UnregisterApplicationRestart();
Application Recovery and Restart provides time-saving functionality for developers looking to improve application reliability and stability. Use application recovery to make sure your application fails gracefully without losing or corrupting data. Use application restart to minimize downtime and increase your confidence in unattended server applications.