如何检测应用程序何时终止? [英] How to detect when application terminates?

查看:64
本文介绍了如何检测应用程序何时终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对我初始问题的跟进想展示我的发现并寻求更正、想法和见解.我的发现(或者更确切地说是解释)来自人们对我之前问题的回答,阅读 MSDN .NET 3.5 文档和调试 .NET 3.5 代码.我希望这对像我一样想知道如何检测应用程序何时终止的人来说是有价值的.

This is a follow up to my initial question and I would like to present my findings and ask for corrections, ideas and insights. My findings (or rather interpretations) come from people's answers to my previous question, reading MSDN .NET 3.5 documentation and debugging .NET 3.5 code. I hope this will be of value to someone who was wondering like me how to detect when an application terminates.

事件:

  • System.AppDomain.CurrentDomain.ProcessExit:在进程退出时引发,例如在默认 AppDomain 和其他所有内容都被卸载后 [总执行时间限制为 3 秒!].对于 WPF,请改用 System.Windows.Application.Exit.对于 Windows 窗体,在 main 方法中的 Application.Run(...) 之后运行代码.

  • System.AppDomain.CurrentDomain.ProcessExit: raised when process exits, e.g. after the default AppDomain and everything else was unloaded [Total execution time is limited to just 3 seconds!]. For WPF, use System.Windows.Application.Exit instead. For Windows Forms, run code after Application.Run(...) in main method.

System.AppDomain.CurrentDomain.DomainUnload:当除默认 AppDomain 之外的 AppDomain 卸载时引发,例如使用单元测试框架(MbUnit 和 TestDriven.NET)运行类时.

System.AppDomain.CurrentDomain.DomainUnload: raised when an AppDomain other than default AppDomain unloads, e.g. when running classes with unit testing frameworks (MbUnit with TestDriven.NET).

System.AppDomain.CurrentDomain.UnhandledException:(如果在默认AppDomain 中处理:)为任何线程中的任何未处理的异常引发,无论AppDomain 线程在其中启动.这意味着,这可以用作所有未处理异常的捕获全部.

System.AppDomain.CurrentDomain.UnhandledException: (if handled in default AppDomain:) raised for any unhandled exception in any thread, no matter what AppDomain the thread started in. This means, this can be used as the catch-all for all unhandled exceptions.

System.Windows.Application.Exit:当 WPF 应用程序(即默认的 AppDomain)正常退出时引发.覆盖 System.Windows.Application.OnExit 以利用它.

System.Windows.Application.Exit: raised when WPF application (i.e. the default AppDomain) exits gracefully. Override System.Windows.Application.OnExit to take advantage of it.

终结器(C# 中的析构函数):当垃圾收集器释放非托管资源时运行.[总执行时间有限!].

Finalizers (destructors in C#): run when garbage collector frees unmanaged resources. [Total execution time is limited!].

事件顺序:

WPF 应用:优雅退出

WPF application: graceful exit

  1. System.Windows.Application.Exit
  2. System.AppDomain.CurrentDomain.ProcessExit
  3. 终结者

WPF 应用程序:未处理的异常

WPF application: unhandled exception

  1. System.AppDomain.CurrentDomain.UnhandledException

MbUnit 在 TestDriven.NET 中运行:通过测试(正常退出)

MbUnit running inside TestDriven.NET: passed test (graceful exit)

  1. System.AppDomain.CurrentDomain.DomainUnload
  2. 终结者

MbUnit 在 TestDriven.NET 中运行:测试失败(未处理的异常由 MbUnit 处理)

MbUnit running inside TestDriven.NET: failed test (unhandled exceptions are handled by MbUnit)

  1. AppDomain.CurrentDomain.DomainUnload
  2. 终结者

问题:

  • 我的解释/发现是否正确?
  • 你知道我掌握的更多细节吗?被遗漏了?例如.总数是多少终结器的执行时间?
  • 你知道其他事件吗/我知道的想法?
  • 那里有什么事件以及它们在其他应用程序中以什么顺序引发,例如Windows 窗体、Web 服务、ASP.NET 网站等?

推荐答案

在ssg31415926的问答提示下(这个问题有点反了),还有Application.SessionEnding 当用户注销或关闭时调用.它在 Exit 事件之前调用.

Prompted by ssg31415926's question/answer (this question is a bit reversed), there's also Application.SessionEnding which is called when the when the user logs off or shuts down. It is called before the Exit event.

这篇关于如何检测应用程序何时终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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