如何退出Windows Forms/C#程序 [英] How to exit a Windows Forms/C# program

查看:81
本文介绍了如何退出Windows Forms/C#程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A类具有一个Form1(System.Windows.Forms.Form的子类)成员.

Class A has a Form1 (subclass of System.Windows.Forms.Form) member.

class A {
        Form1 form;
        public A()
        {
            form = new Form1();
            form.Show();
        }
    }
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        A a = new A();
        Application.Run();
    }

问题是我不知道如何退出程序.在处理 Form.Closed 事件或调用A.Dispose()时,我曾尝试过Application.Exit(),但是Windows任务管理器仍然列出了我的程序进程.

The problem is I do not know how to exit the program. I have tried Application.Exit() when handling the Form.Closed event or call A.Dispose(), but the Windows Task Manager still lists the process of my program.

我如何完成该程序?

推荐答案

按照 Microsoft您应该使用此:

Application.Run(a.Form);

因为 MSDN 指出

大多数Windows Forms开发人员将不需要使用此版本的方法.您应该使用Run(Form)重载来启动具有主窗体的应用程序,以使该应用程序在主窗体关闭时终止.

Most Windows Forms developers will not need to use this version of the method. You should use the Run(Form) overload to start an application with a main form, so that the application terminates when the main form is closed.

这篇关于如何退出Windows Forms/C#程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆