如果使用任务管理器关闭应用程序,c# 窗体关闭事件是否会起作用?

Will c# form closing event work if the application is closed with task manager?

我有一个带有表单关闭事件的表单,该事件保存了应用程序的最后一次使用时间。

我的问题是'Will the form closing event still work if the application is force closed with task manager'?

简短的回答是否定的。当您的应用程序被任务管理器终止时,位于您的 FormClosing 事件中的代码将永远不会执行。

但是您可以通过其他方式捕获此事件。任务管理器调用 Windows API ExitProcess 来终止进程。您可以查看 ProcessExit and DomainUnload events of the AppDomain class.

对于 WinForms 应用程序,有 Application.ApplicationExit 事件。

也许这篇文档也能帮到你:

How to know when the Task Manager kills your app.