捕获应用程序退出事件 – WinForms

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

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

要么

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

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

在Program.cs文件中,您必须 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()); } 

您还可以在MainForm上使用Disposed事件(Application.Run方法中使用的表单)。