006——C#关闭窗口 添加FormClosing事件

(一)参考文献:C# 添加FormClosing事件

(二)在  项目名.Designer.cs中的InitializeComponent()添加:

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMain_FormClosing);

 

 

 

 

 

 (三)在   项目名.cs中添加:

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("你确定要退出?数据可能会丢失!", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                e.Cancel = false;     //关闭窗体
            } 
            else
                e.Cancel = true;     //返回窗体
        }

 

 

 

 

 

  • this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出;
  • Application.Exit();  强制所有消息中止,退出所有的窗体,但是若有托管线程(非主线程),也无法干净地退出;
  • Application.ExitThread(); 强制中止调用线程上的所有消息,同样面临其它线程法正确退出的问题;
  • System.Environment.Exit(0);   这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净。
  • Thread_VIEW.Abort(); //关闭Thread_VIEW线程
    Thread_SAVE.Abort(); //关闭Thread_SAVE线程

posted @ 2019-10-07 16:04  陆小果哥哥  阅读(4809)  评论(0编辑  收藏  举报