zoukankan      html  css  js  c++  java
  • C#中WinForm程序退出方法技巧总结

    C#中WinForm程序退出方法技巧总结 

    一、关闭窗体 

    在c#中退出WinForm程序包括有很多方法,如:this.Close(); Application.Exit();Application.ExitThread(); System.Environment.Exit(0); 等他们各自的方法不一样,下面我们就来详细介绍一下。 

    1.this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出; 

    2.Application.Exit();  强制所有消息中止,退出所有的窗体,但是若有托管线程(非主线程),也无法干净地退出; 

    3.Application.ExitThread(); 强制中止调用线程上的所有消息,同样面临其它线程无法正确退出的问题; 

    4.System.Environment.Exit(0);   这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净

    二、登录窗体与主窗体的关闭 

    有许多人在用做c# 做登录窗体时会遇到这样的问题,登录成功后当前登录Form关闭,打开Main窗体,但Main窗体点关闭按钮后程序仍在进程仍旧在运行。 

    事实上,关闭Main窗体,只是关闭了Main窗体的线程,并没有关闭程序的主线程,即程序的主线程为登录From。 

    方法一:

            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new FLogin());
                FLogin f1 = new FLogin();
                if (f1.ShowDialog() == DialogResult.OK)
                {
                    Application.Run(new From1());
                }
    
            }
    

    登录窗体的 登录按钮

            private void button1_Click(object sender, EventArgs e)
            {
                if ((userID.Text == "test") && (password.Text == "test"))
                {
                    From1 f = new From1();
                    f.Show();
    
    
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
    
                }
    
            }
    
  • 相关阅读:
    COMMIT WORK AND WAIT 是在WAIT什么
    BINARY SEARCH in read table statement
    SAP 金额在表中的存储及货币转换因子
    REUSE_ALV_POPUP_TO_SELECT的使用技巧
    SAPScript、Smartforms动态打印图像或者背景图片
    SAP_Web_Service开发配置
    SAP中关于用户IP信息的获取(转载)
    DevExpress控件开发常用要点(项目总结版)
    鼠标指向表格时 显示更多信息 toolTipController1
    DevExpress组件之——TreeList组件
  • 原文地址:https://www.cnblogs.com/rosesmall/p/8854792.html
Copyright © 2011-2022 走看看