日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Windows 7 应用程序崩溃恢复

發(fā)布時間:2025/5/22 windows 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows 7 应用程序崩溃恢复 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

???? 從Vista 到Windows 7 這兩款操作系統(tǒng)都帶有應用程序恢復和重啟(ARR)功能,利用這個特性可以在應用程序處于無響應甚至崩潰狀態(tài)時,保存當前正在處理的數(shù)據(jù),并將應用程序以及之前的數(shù)據(jù)恢復。本篇我們將利用Windows API Code Pack 來實現(xiàn)這一功能。

???? 首先,我們來創(chuàng)建一個簡單的WPF程序。在應用程序加載時需要注冊(Register)ARR,當應用程序關閉時也需要將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>

注冊ARR

public MainWindow() {InitializeComponent();RegisterForRestartRecovery();... ... }

注銷ARR

private void closeBtn_Click(object sender, RoutedEventArgs e) {UnRegisterRestartRecovery();App.Current.Shutdown(); }

???? 在項目中加入Microsoft.WindowsAPICodePack.dll,并添加using Microsoft.WindowsAPICodePack.ApplicationServices; 命名空間。接下來我們開始編寫RegisterForRestartRecovery 和UnRegisterRestartRecovery 方法。

???? 在RegisterForRestartRecovery 方法中要分別創(chuàng)建Restart 和Recovery 設置(Settings)。在RestartSettings 中可以設置命令行(“restart”),以及Restart 限制條件。在本例中如果應用程序崩潰是因為PC 重啟或安裝系統(tǒng)補丁則不會發(fā)生Restart 功能。最后要通過ApplicationRestartRecoveryManager 類將Restart 和Recovery 設置分別注冊。

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); }

注銷方式使用UnregisterApplicationRestar和 UnregisterApplicationRecovery 兩種方法即可。

private void UnRegisterRestartRecovery() {ApplicationRestartRecoveryManager.UnregisterApplicationRestart();ApplicationRestartRecoveryManager.UnregisterApplicationRecovery(); }

???? 在應用程序恢復過程中還需要編寫一個恢復過程,即RegisterForRestartRecovery 方法提到的PerformRecovery。首先可以通過ApplicationRecoveryInProgress 方法判斷恢復過程是否在進行。如果恢復過程被用戶取消了,則可以將應用程序進程殺掉,并通過ApplicationRecoveryFinished 方法設置恢復過程是否完成。

private int PerformRecovery(object state) {bool isCanceled = ApplicationRestartRecoveryManager.ApplicationRecoveryInProgress();if (isCanceled){ApplicationRestartRecoveryManager.ApplicationRecoveryFinished(false);}//recovery your work here ...ApplicationRestartRecoveryManager.ApplicationRecoveryFinished(true);return 0; }

???? 至此,應用程序的恢復就完成了,大家可以下載代碼進行測試。另,當程序啟動后要等待60秒再點擊“Application Crash” 按鍵。

源碼下載

AppRestartRecovery.zip

轉(zhuǎn)載于:https://www.cnblogs.com/gnielee/archive/2011/01/04/windows7-application-restart-recovery.html

總結(jié)

以上是生活随笔為你收集整理的Windows 7 应用程序崩溃恢复的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。