默认 AppDomain 的卸载事件?

发布于 2024-10-09 11:51:25 字数 229 浏览 8 评论 0原文

我需要在任何 AppDomain 卸载时触发一个事件 - 包括进程的默认事件。 AppDomain.DomainUnload 的问题是它仅针对非默认 AppDomain 触发。此外,AppDomain.ProcessExit 的执行时间有限,我不能依赖它。

任何有关我如何实现这一目标的建议将不胜感激!

(或者,当后台线程 (Thread.IsBackground == True) 工作时也会触发一个事件。)

I need to have an event fired whenever any AppDomain unloads - including the default one of the process. The problem with AppDomain.DomainUnload is that it only fires for non-default AppDomains. Furthermore, AppDomain.ProcessExit has limited execution time, which I cannot rely on.

Any suggestions as to how I can achieve this would be greatly appreciated!

(Alternatively, having an event fired when a background thread (Thread.IsBackground == True) works too.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悟红尘 2024-10-16 11:51:25

为什么不把代码放在Main的末尾呢?

至于事件:

不,卸载应用程序域时不会执行任何事件。如果您是库开发人员,您可以将析构函数添加到您的入门类中。但请注意,所有内容可能无法正确收集。阅读此答案:https://stackoverflow.com/a/2735431/70386

Why do you not put your code in the end of Main?

As for an event:

No there is no event which will get executed when the application domain is unloaded. If you are a library developer you add a destructor to your entry class. But beware that everything might not be collected properly. Read this answer: https://stackoverflow.com/a/2735431/70386

风和你 2024-10-16 11:51:25

http://msdn.microsoft .com/en-us/library/system.appdomain.domainunload%28v=VS.90%29.aspx ..但这又是在域卸载之前调用的。

摘自 http://msdn.microsoft.com/en-us /library/system.appdomain.unload.aspx
在 .NET Framework 2.0 版中,有一个专门用于卸载应用程序域的线程。这提高了可靠性,尤其是在托管 .NET Framework 时。当线程调用 Unload 时,目标域被标记为卸载。专用线程尝试卸载域,并且域中的所有线程都被中止。如果线程没有中止,例如因为它正在执行非托管代码,或者因为它正在执行finally 块,那么在一段时间后,最初调用 Unload 的线程中将引发 CannotUnloadAppDomainException。如果无法中止的线程最终结束,则不会卸载目标域。因此,在 .NET Framework 2.0 版中,不能保证域能够卸载,因为可能无法终止正在执行的线程。

http://msdn.microsoft.com/en-us/library/system.appdomain.domainunload%28v=VS.90%29.aspx ..but again this is called before the domain is unloaded.

Taken from http://msdn.microsoft.com/en-us/library/system.appdomain.unload.aspx
In the .NET Framework version 2.0 there is a thread dedicated to unloading application domains. This improves reliability, especially when the .NET Framework is hosted. When a thread calls Unload, the target domain is marked for unloading. The dedicated thread attempts to unload the domain, and all threads in the domain are aborted. If a thread does not abort, for example because it is executing unmanaged code, or because it is executing a finally block, then after a period of time a CannotUnloadAppDomainException is thrown in the thread that originally called Unload. If the thread that could not be aborted eventually ends, the target domain is not unloaded. Thus, in the .NET Framework version 2.0 domain is not guaranteed to unload, because it might not be possible to terminate executing threads.

鯉魚旗 2024-10-16 11:51:25

在主应用程序周围使用包装器/启动器怎么样?该 AppDomain 可以注意到其子进程何时结束(通过阻塞、轮询或回调),并且可以执行所需的任何其他操作。

同样,如果 main() 包括启动一个执行所有重要工作的单个线程,那么 main() 还可以执行所需的任何其他工作。

如果您能解释一下您需要做什么来响应每个 AppDomain 终止,也许会有帮助?您要求做的事情可能在 AppDomain 级别不合适。

What about using a wrapper/launcher around your main application? That AppDomain could notice when its child ends (via blocking, polling or callback) and could perform whatever additional operations are needed.

Likewise, if main() consists of starting a single thread which does all the important stuff, then main() could also perform whatever additional work is needed.

Perhaps it would help if you could explain what you need to do in response to each AppDomain termination? It could be that what you are asking to do just isn't appropriate at the AppDomain level.

山田美奈子 2024-10-16 11:51:25

如果您的目标是干净地处理某些后台线程使用的对象,那么我的建议是:

  1. 当您想要退出应用程序时,向后台线程发出信号,告知它们应该清理并停止。

  2. 在主线程中,使用 Thread.Join 等待所有后台线程完成并退出。

If your goal here is to cleanly dispose of objects that are used by some background threads, then my suggestion would be:

  1. When you want your want to exit the application, signal to the background threads that they should clean up and stop.

  2. From the main thread, use Thread.Join to wait for all the background threads to finish and exit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文