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

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

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

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)  
        {  
            PromptForSave();  
            Application.Exit();  
            Close();  
        }  
 
        private void openToolStripMenuItem_Click(object sender, EventArgs e)  
        {  
            PromptForSave();  
 
            CommonOpenFileDialog cfd = new CommonOpenFileDialog();  
            cfd.Filters.Add(new CommonFileDialogFilter("Text files", ".txt"));  
 
            CommonFileDialogResult result = cfd.ShowDialog();  
 
            if (result == CommonFileDialogResult.OK)  
            {  
                internalLoad = true;  
                Form1.CurrentFile.Load(cfd.FileName);  
                textBox1.Text = CurrentFile.Contents;  
                internalLoad = false;  
 
                UpdateAppTitle();  
 
 
            }  
        }  
 
        private void PromptForSave()  
        {  
            if (Form1.CurrentFile.IsDirty)  
            {  
                //请用户保存。  
                DialogResult dr = MessageBox.Show(this, "Current document has changed. Would you like to save?", "Save current document", MessageBoxButtons.YesNoCancel);  
 
                if (dr == DialogResult.Cancel)  
                    return;  
                else if (dr == DialogResult.No)  
                {  
                    // 什么也不做  
                }  
                else if (dr == DialogResult.Yes)  
                {  
                    // 当前文件是否有一个名字?  
                    if (string.IsNullOrEmpty(Form1.CurrentFile.Filename))  
                    {  
                        CommonSaveFileDialog saveAsCFD = new CommonSaveFileDialog();  
                        saveAsCFD.Filters.Add(new CommonFileDialogFilter("Text files", ".txt"));  
 
                        if (saveAsCFD.ShowDialog() == CommonFileDialogResult.OK)  
                        {  
                            Form1.CurrentFile.Save(saveAsCFD.FileName);  
                            UpdateAppTitle();  
                        }  
                        else 
                            return;  
                    }  
                    else 
                    {  
                        // 暂时保存  
                        Form1.CurrentFile.Save(CurrentFile.Filename);  
                        UpdateAppTitle();  
                    }  
                }  
            }  
        }  
 
        private void textBox1_TextChanged(object sender, EventArgs e)  
        {  
            if (!internalLoad && Form1.CurrentFile != null)  
            {  
                Form1.CurrentFile.IsDirty = true;  
                Form1.CurrentFile.Contents = textBox1.Text;  
                UpdateAppTitle();  
            }  
        }  
 
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)  
        {  
            // 当前文件是否有一个名字?  
            if (string.IsNullOrEmpty(Form1.CurrentFile.Filename))  
            {  
                CommonSaveFileDialog saveAsCFD = new CommonSaveFileDialog();  
                saveAsCFD.Filters.Add(new CommonFileDialogFilter("Text files", ".txt"));  
 
                if (saveAsCFD.ShowDialog() == CommonFileDialogResult.OK)  
                {  
                    Form1.CurrentFile.Save(saveAsCFD.FileName);  
                    UpdateAppTitle();  
                }  
                else 
                    return;  
            }  
            else 
            {  
                // 暂时保存  
                Form1.CurrentFile.Save(Form1.CurrentFile.Filename);  
                UpdateAppTitle();  
            }  
        }  
 
        private void UpdateAppTitle()  
        {  
            string dirtyState = "";  
 
            if (Form1.CurrentFile.IsDirty)  
                dirtyState = "*";  
 
            if (string.IsNullOrEmpty(Form1.CurrentFile.Filename))  
                this.Text = string.Format("{0}{1} - {2}", "Untitled", dirtyState, AppTitle);  
            else 
                this.Text = string.Format("{0}{1} - {2}", Path.GetFileName(Form1.CurrentFile.Filename), dirtyState, AppTitle);  
 
            if (recovered)  
                this.Text += " (RECOVERED FROM CRASH)";  
        }  
 
        private void crashToolStripMenuItem_Click(object sender, EventArgs e)  
        {  
            Crash();  
        }  
 
        private void undoToolStripMenuItem_Click(object sender, EventArgs e)  
        {  
            textBox1.Undo();  
        }  
 
        private void cutToolStripMenuItem1_Click(object sender, EventArgs e)  
        {  
            textBox1.Cut();  
        }  
 
        private void copyToolStripMenuItem1_Click(object sender, EventArgs e)  
        {  
            textBox1.Copy();  
        }  
 
        private void pasteToolStripMenuItem1_Click(object sender, EventArgs e)  
        {  
            textBox1.Paste();  
        }  
 
        private void selectAllToolStripMenuItem1_Click(object sender, EventArgs e)  
        {  
            textBox1.SelectAll();  
        }  
 
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)  
        {  
            MessageBox.Show(this, "重新启动和恢复的应用演示", "Windows API的.NET Framework代码包");  
        }  
 
        private void timer1_Tick(object sender, EventArgs e)  
        {  
            TimeSpan span = DateTime.Now - startTime;  
            timerLabel.Text = string.Format("App running for {0}s", (int)span.TotalSeconds);  
        }  
 
        private void Form1_Load(object sender, EventArgs e)  
        {  
            startTime = DateTime.Now;  
        }  
    }  

4.启动调试运行后效果如下图:


60秒后的界面(程序已经“安全”崩溃!)

单击应用程序重新启动恢复->崩溃!后

稍等片刻

之后程序重启后恢复正常,还是之前我编写的文件内容,成功实现了灾难恢复的功能:

 

 

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