Application.ApplicationExit事件是否可以在非Winforms应用程序中通知退出? [英] Does Application.ApplicationExit event work to be notified of exit in non-Winforms apps?

查看:776
本文介绍了Application.ApplicationExit事件是否可以在非Winforms应用程序中通知退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的代码库需要在应用程序退出时收到通知。所以我们订阅了System.Window.Forms.Application.ApplicationExit事件。这对于Winforms应用程序来说可以很好的工作,但它也适用于其他类型的应用程序,如控制台应用程序,服务和Web应用程序(如ASP.NET)?命名空间会表明它不会,并且当 Application.Exit()被调用(明确地或暗示地)时可能会被提升,这可能是不正确的这些其他情况。

Our code library needs to be notified when the application is exiting. So we have subscribed to the System.Window.Forms.Application.ApplicationExit event. This works nicely for Winforms apps, but does it also work for other types of applications such as console apps, services, and web apps (such as ASP.NET)? The namespace would suggest that it doesn't, and it presumably gets raised when Application.Exit() is called (explicitly or implictly), which may not be correct to call for these other cases.

是否还有其他一些事件在其他情况下会更好,哪些更普遍(如果它适用于Winforms也是好的)?例如,当 Environment.Exit()被调用(控制台应用程序)时是否有事件?

Is there some other event which would be better in these other cases or which would be more universal (great if it works for Winforms, too)? For example, is there an event for when Environment.Exit() is called (console app)?

I发现在System.Diagnostic.Process中提到了一个Exited事件,但这似乎是用于监视另一个进程的退出,并且它似乎没有被进程自身接收(例如, Process.GetCurrentProcess()。Exited + = Process_Exited; Process.GetCurrentProcess()。EnableRaisingEvents = true; )。我会认为它可能只有在进程实际退出之后才能被提升,这样就不行了。

I found a mention of an Exited event in System.Diagnostic.Process, but this appears to be for monitoring the exit of another process, and it does not appear to be received by a process about itself (for example, Process.GetCurrentProcess().Exited += Process_Exited; Process.GetCurrentProcess().EnableRaisingEvents = true;). I would think it might only be raised after the process has actually exited, so that wouldn't work.

这是特别适用于.NET 2.0和C#。 >

This is particularly for .NET 2.0 and C#.

推荐答案

我们终于找到了更多的内容(但是到那时,我的机器已经重建,丢失了cookies到我未注册的个人资料,希望这样将会遇到这个答案)

We finally found more about this (but by then my machine had been rebuilt and lost the cookies to my unregistered profile here; hopefully, it will let met post this answer).

进一步调查最终发现了一些我们发现有用的事件:

Further investigation eventually found a few more events which we have found helpful:

System.Windows.Forms.Application.ThreadExit - 消息循环退出时触发
System.Windows.Forms.Application.ApplicationExit - 所有邮件循环退出时触发
System.AppDomain.CurrentDomain.DomainUnload - 当默认值以外的域退出$ b时触发$ b System.AppDomain.CurrentDomain.ProcessExit - 当默认应用程序域退出
System.AppDomain.CurrentDomain.UnhandledException - 发生时一个未捕获的异常发生,结束了应用程序。

System.Windows.Forms.Application.ThreadExit - Fires when a message loop exits System.Windows.Forms.Application.ApplicationExit - Fires when all message loops exit System.AppDomain.CurrentDomain.DomainUnload - Fires when a domain other than the default exits System.AppDomain.CurrentDomain.ProcessExit - Fires when the default app domain exits System.AppDomain.CurrentDomain.UnhandledException - Fires when an uncaught exception occurs, ending the app.

只有一个 DomainUnload ProcessExit 事件对于特定的应用程序域是可能的,具体取决于它是进程的默认(顶级)域还是作为子域创建(例如,在Web服务器上)。如果一个应用程序不知道它可能是什么(在我们的情况下),它需要订阅两者,如果它想要捕获实际的卸载本身。此外,似乎 UnhandledException (从.NET2.0开始总是致命的)可能会阻止其他两个事件,因此可能是第三种情况来处理。这些三个事件应该适用于任何.NET应用程序。

Only one of the DomainUnload or ProcessExit events are possible for a given app domain, depending on whether it is the default (top-level) domain for the process or was created as a subdomain (eg. on a web server). If an application doesn't know which it might be (as in our case), it needs to subscribe to both if it wants to catch the actual unload for itself. Also, it appears that UnhandledException (which as of .NET2.0 is always fatal) may prevent the other two events, so that may be a third case to handle. These three events should work for any .NET application.

有一个警告: ProcessExit 的执行时间是有限(约4秒?),所以在该事件处理程序中可能无法进行广泛的最终工作。

There is a caveat that the execution time for ProcessExit is bounded (about 4 seconds?), so it may not be possible to do extensive "final" work in that event handler. It needs to be something which can be done quickly.

应用程序事件仅适用于WinForms应用程序(我们怀疑但是,它们可能不适用于纯WPF应用程序)。命名可能是误导性的,因为它们以具有某些假设的最基本的正常使用命名。 ThreadExit 与实际的 System.Threading.Thread 无关,而是与消息循环())和 ApplicationExit 类似地涉及一个或多个UI线程上的应用程序窗体的集合。通常,一旦从线程的入口方法调用,调用$ code Application.Run()就会返回结果,线程本身就结束了。一旦所有的UI线程已经退出,WinForms应用程序通常都会完成并退出。

The Application events only apply to WinForms applications (we suspect they may not apply in pure WPF applications, however). The naming can be misleading because they are named for their most basic normal usage which has certain assumptions. ThreadExit does not relate to the actual System.Threading.Thread but rather to the message loop (Application.Run())) of a UI thread, and ApplicationExit similarly relates to the collection of application Forms on one or more UI threads. Normally, once the call to Application.Run() returns, called from the entry method of a thread, the entry method quickly concludes and the thread itself then ends. And once all UI threads have exited, a WinForms app is usually all done and exits.

另一个注意事项是 System.Windows.Forms .Application.ThreadException 事件。 Windows消息循环可以配置为捕获在处理消息时发生的异常,并发送此事件,而不是让它们被忽略(因此致命)异常。捕获这些异常允许消息循环(和该UI线程)继续运行(在中止当前消息处理程序之后)。对于给定的线程(预订覆盖任何以前的用户),任何时候只能有一个订阅者进行此事件,并且必须在进入消息循环之前创建和订阅任何表单之前进行配置。有关更多信息,请参阅此事件的MSDN帮助和 System.Windows.Forms.Applicaton.SetUnhandledExceptionMode()

Another event of note is the System.Windows.Forms.Application.ThreadException event. A Windows message loop can be configured to catch exceptions which occur in handling a message and send this event rather than let them be uncaught (and thus fatal) exceptions. Catching these exceptions allows the message loop (and that UI thread) to continue running (after aborting the current message handler). There can be only one subscriber to this event at any time for a given thread (subscriptions overwrite any previous subscriber), and it must be configured before any Form is created and subscribed before entering the message loop. See the MSDN help for this event and System.Windows.Forms.Applicaton.SetUnhandledExceptionMode() for more info.

这篇关于Application.ApplicationExit事件是否可以在非Winforms应用程序中通知退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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