From Vista to Windows 7, these two operating systems both feature application Recovery and restart (ARR), which can be used when the application is in a unresponsive or even crashed state, save the data that is currently being processed and restore the application and previous data. This article uses the Windows API Code Pack to implement this function.
First, create a simple WPF program. You need to Register (Register) ARR when loading an application. When the application is closed, you also need to cancel the ARR.
<Window x:Class="AppRestartRecovery.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Button x:Name="crashBtn" Content="Application Crash" Margin="169,104,172,168" Click="crashBtn_Click"/> <Button x:Name="closeBtn" Content="Close Application" Margin="169,189,172,83" Click="closeBtn_Click"/> </Grid></Window>
Register ARR
public MainWindow(){ InitializeComponent(); RegisterForRestartRecovery(); ... ...}
Log out of ARR
private void closeBtn_Click(object sender, RoutedEventArgs e){ UnRegisterRestartRecovery(); App.Current.Shutdown();}
Add Microsoft. WindowsAPICodePack. dll to the project, and add using Microsoft. WindowsAPICodePack. ApplicationServices; namespace. Next, we will compile the RegisterForRestartRecovery and UnRegisterRestartRecovery methods.
In the RegisterForRestartRecovery method, you must create Restart and resettings Settings respectively ). You can set the command line ("restart") and Restart restrictions in RestartSettings. In this example, if the application crashes because the PC is restarted or the system patch is installed, the Restart function is not enabled. Finally, you must register the Restart and Recovery settings respectively through the ApplicationRestartRecoveryManager class.
private void RegisterForRestartRecovery(){ RestartSettings restartSettings = new RestartSettings("restart", RestartRestrictions.NotOnReboot | RestartRestrictions.NotOnPatch); ApplicationRestartRecoveryManager.RegisterForApplicationRestart(restartSettings); RecoveryData data = new RecoveryData(new RecoveryCallback(PerformRecovery), null); RecoverySettings recoverySettings = new RecoverySettings(data, 0); ApplicationRestartRecoveryManager.RegisterForApplicationRecovery(recoverySettings);}
You can use either UnregisterApplicationRestar or UnregisterApplicationRecovery to log out.
private void UnRegisterRestartRecovery(){ ApplicationRestartRecoveryManager.UnregisterApplicationRestart(); ApplicationRestartRecoveryManager.UnregisterApplicationRecovery();}
In the application recovery process, you also need to write a recovery process, that is, the restore mrecovery mentioned in the RegisterForRestartRecovery method. You can use ApplicationRecoveryInProgress to determine whether the restoration process is in progress. If the restoration process is canceled, you can kill the application process and use the ApplicationRecoveryFinished method to set whether the restoration process is complete.
private int PerformRecovery(object state){ bool isCanceled = ApplicationRestartRecoveryManager.ApplicationRecoveryInProgress(); if (isCanceled) { ApplicationRestartRecoveryManager.ApplicationRecoveryFinished(false); } //recovery your work here ... ApplicationRestartRecoveryManager.ApplicationRecoveryFinished(true); return 0;}
Now, the recovery of the application is complete. You can download the code for testing. In addition, after the program starts, wait 60 seconds and then click the "Application Crash" button.
Source code download
AppRestartRecovery.zip