Sep 24

C#的窗体(WinForm)退出杂谈

piikee , 09:38 , 计算机相关 » .net专区 , 评论(0) , 引用(0) , 阅读(1311) , 本站原创 | |
1、Winform点击退出按钮,需要确认再退出:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //  关闭所有的线程
                this.Dispose();
                this.Close();
            }
            else
                e.Cancel = true;
        }


2、程序退出的方法:


this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出;
Application.Exit();  强制所有消息中止,退出所有的窗体,但是若有托管线程(非主线程),也无法干净地退出;
Application.ExitThread(); 强制中止调用线程上的所有消息,同样面临其它线程无法正确退出的问题;
System.Environment.Exit(0);   这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净。
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]