Windows 7 application crash recovery

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.