15

单击应用程序停止时,这是否可以捕获 Windows 窗体关闭事件。我知道应用程序停止工作,但是当应用程序关闭时,是否有任何可用的方法在任何情况下都会触发?我从谷歌找到了这些方法,但它们不起作用:

 AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);  

或者

  Application.ApplicationExit += new EventHandler(this.OnApplicationExit); 

单击应用程序停止按钮时是否可以获得关闭事件?

4

1 回答 1

23

In Program.cs file, you must have those methods before Application.Run :

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
        Application.Run(new MainForm());
    }

You can also use the Disposed event on the MainForm (the form used in Application.Run method).

于 2012-05-14T08:11:44.853 回答