Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
当前位置: 主页 > vs2010教程 >

基于Visual C#2010开发Windows7应用程序遇到灾难性重新启动恢复的演示

时间:2010-08-06 18:54来源:未知 作者:admin 点击:

在我们平常使用各种各样的软件的时候,特别是在使用软件进行工作的时候,最害怕的事情就是软件突然崩溃,自己的工作成果化为乌有。像在Microsoft offic word2007就提供了这种灾难性回复的支持,例如在你写了一段文档数据后在还没有保存的情况下突然断电或者操作故障导致软件或者操作系统崩溃,那是不是之前我们写的文档就丢失了呢,不是的,在你再次开机重启Microsoft offic word2007后你会惊喜的发现你上次丢失的文件完好无损的展现在您面前,提示您是否保存,看来这种功能还真的很有用,下面我们来看看在基于Visual C#2010开发应用程序遇到灾难性重新启动恢复的演示的开发过程。来在自己的应用程序上定制自己的灾难恢复系统。

1.启动VS2010。

2.创建一个AppRestartRecoveryDemo程序,定制下列界面:插入一个menuStrip1,一个statusStrip1,一个timer1


3.具体实现代码如下(解释见代码中注释部分):

using System.Windows.Forms;  
using System.Diagnostics;  
using System;  
using System.Threading;  
using System.Timers;  
using Microsoft.WindowsAPICodePack.Shell;  
using System.IO;  
using Microsoft.WindowsAPICodePack.ApplicationServices;  
using Microsoft.WindowsAPICodePack.Dialogs;  
 
namespace Microsoft.WindowsAPICodePack.Samples.AppRestartRecoveryDemo  
{  
    public partial class Form1 : Form  
    {  
        private static string AppTitle = "应用程序重启/恢复的演示-尹成的杰作";   
        private static FileSettings CurrentFile = new FileSettings();  
        private static string RecoveryFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "AppRestartRecoveryDemoData.xml");  
        private static string DataSeparatorString = "@@@@@@@@@@";  
 
        //  
        private bool internalLoad = false;  
        private bool recovered = false;  
        private DateTime startTime;  
 
        public Form1()  
        {  
            Debug.WriteLine("ARR: Demo started");  
 
            InitializeComponent();  
 
              
            Form1.CurrentFile.IsDirty = false;  
 
            UpdateAppTitle();  
 
            RegisterForRestart();             
            RegisterForRecovery();  
 
 
            statusLabel.Text = "注册为重新启动/恢复申请成功。该应用程序崩溃之前等待60秒。";  
 
            //SetupTimerNotifyForRestar设置了一个timer来计时当60秒过去了就开始报警,这表明WER将在程序崩溃后重新启动,  
            SetupTimerNotifyForRestart();  
 
            // 如果我们开始/重新启动命令行参数   
            // 然后我们自动重新启动,并应  
            // 尝试恢复之前的会话。  
            if (System.Environment.GetCommandLineArgs().Length > 1 && System.Environment.GetCommandLineArgs()[1] == "/restart")  
            {  
                recovered = true;  
                RecoverLastSession(System.Environment.GetCommandLineArgs()[1]);  
            }  
        }  
 
        private void SetupTimerNotifyForRestart()  
        {  
            // 60秒时的响声已过。  
            System.Timers.Timer notify = new System.Timers.Timer(60000);  
            notify.Elapsed += new ElapsedEventHandler(NotifyUser);  
            notify.AutoReset = false; // 只有蜂鸣。  
            notify.Enabled = true;  
        }  
 
        private void NotifyUser(object source, ElapsedEventArgs e)  
        {  
            statusLabel.Text = "这是\"安全\"崩溃了! (单击应用程序重新启动恢复->崩溃!)";  
        }  
 
        private void Crash()  
        {  
            Environment.FailFast("ARR Demo intentional crash.");  
        }  
 
        private void RegisterForRestart()  
        {  
            //注册自动重新启动,如果应用程序不是以任何理由重新启动系统或系统更新而终止。  
            ApplicationRestartRecoveryManager.RegisterForApplicationRestart(  
                new RestartSettings("/restart", RestartRestrictions.NotOnReboot | RestartRestrictions.NotOnPatch));  
 
            Debug.WriteLine("ARR: Registered for restart");  
        }  
 
        private void RegisterForRecovery()  
        {  
            //我们使用静态变量“CurrentFile”,以确定应用程序的当前状态。  
            //由于这是正在申请注册的启动,在某些情况下,它可能记住这项有意义的初始状态。  
            //另一种做法:在做“自动保存”,每次登记回收,并记住保存当前的状态。  
            RecoveryData data = new RecoveryData(new RecoveryCallback(RecoveryProcedure), null);  
            RecoverySettings settings = new RecoverySettings(data, 0);  
 
            ApplicationRestartRecoveryManager.RegisterForApplicationRecovery(settings);  
 
            Debug.WriteLine("ARR: Registered for recovery");  
        }  
 
        // 这种方法是调用WER  
        private int RecoveryProcedure(object state)  
        {  
            Debug.WriteLine("ARR: Recovery procedure called!!!");  
 
            PingSystem();  
 
            // 在这里做恢复工作。  
            //WER恢复的信号工作仍在进行中。              
            //写入文件的内容,以及一些其他我们需要的数据。  
            File.WriteAllText(RecoveryFile, string.Format("{1}{0}{2}{0}{3}", DataSeparatorString, CurrentFile.Filename, CurrentFile.IsDirty, CurrentFile.Contents));  
 
            Debug.WriteLine("File path: " + RecoveryFile);  
            Debug.WriteLine("File exists: " + File.Exists(RecoveryFile));  
            Debug.WriteLine("Application shutting down...");  
 
            ApplicationRestartRecoveryManager.ApplicationRecoveryFinished(true);  
            return 0;  
        }  
 
 
        //此方法被调用以确保定期WER恢复仍在进行中。  
        private void PingSystem()  
        {  
            // Find out if the user canceled recovery.  
            bool isCanceled = ApplicationRestartRecoveryManager.ApplicationRecoveryInProgress();  
 
            if (isCanceled)  
            {  
                Console.WriteLine("Recovery has been canceled by user.");     
                Environment.Exit(2);  
            }  
        }  
 
 
        private void RecoverLastSession(string command)  
        {  
            if (!File.Exists(RecoveryFile))  
            {  
                MessageBox.Show(this, string.Format("Recovery file {0} does not exist", RecoveryFile));  
                internalLoad = true;  
                textBox1.Text = "Could not recover the data. Recovery data file does not exist";  
                internalLoad = false;  
                UpdateAppTitle();  
                return;  
            }  
 
            // Perform application state restoration   
            // actions here.  
            //执行应用程序状态恢复的方法  
            string contents = File.ReadAllText(RecoveryFile);  
 
            CurrentFile.Filename = contents.Remove(contents.IndexOf(Form1.DataSeparatorString));  
 
            contents = contents.Remove(0, contents.IndexOf(Form1.DataSeparatorString) + Form1.DataSeparatorString.Length);  
 
            CurrentFile.IsDirty = contents.Remove(contents.IndexOf(Form1.DataSeparatorString)) == "True" ? true : false;  
 
            contents = contents.Remove(0, contents.IndexOf(Form1.DataSeparatorString) + Form1.DataSeparatorString.Length);  
 
            CurrentFile.Contents = contents;  
 
            // 给文本赋值  
            textBox1.Text = CurrentFile.Contents;  
 
            // 更新标题  
            UpdateAppTitle();  
 
            // 重置我们的变量,以便下次标题更新我们不显示“恢复”文本  
            recovered = false;  
        }   
  (责任编辑:admin)

Tags:vs2010 Windows7
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价