默认应用程序域的卸载事件? [英] Unload event for the default Application Domain?

查看:29
本文介绍了默认应用程序域的卸载事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Unload 事件,或任何 事件、通知、消息、机制或钩子,我可以使用它在默认值之前收到通知 应用程序域被卸载?

我有代码需要知道应用程序域(几乎总是默认域)何时结束.

注意:我不知道开发人员在使用我的代码时会创建什么样的应用程序.可能是:

  • 控制台应用程序
  • 一个 WinForms 应用程序
  • 一个 ASP.net 应用程序
  • 一个 ASP.net 网站
  • 运行时可调用包装器 (RCW) COM 对象
  • Windows 资源管理器外壳扩展
  • 或 Windows 服务

无论哪种方式,我都需要知道域何时关闭,以便我可以做一些东西".而且我不会要求用户调用任何类型的 "Shutdown""Cleanup" 方法.(此外,建议要求用户自己调用方法并不能回答这个问题:当我正在运行的应用程序域关闭时收到通知).

另见

解决方案

我忘了从 我的其他轻微变化中交叉发布我自己的答案这个问题.最终,答案来自M.A. Hanin 的回答.

没有DomainUnload,但有一个ProcessExit:

类 Contoso{//构造函数公共 Contoso(){//...//捕获域关闭(Hack:疯狂寻找我们可以捕获的东西)如果 (AppDomain.CurrentDomain.IsDefaultAppDomain())AppDomain.CurrentDomain.ProcessExit += MyTerminationHandler;别的AppDomain.CurrentDomain.DomainUnload += MyTerminationHandler;}private void MyTerminationHandler(object sender, EventArgs e){//域正在消亡.序列化我们的价值观this.Dispose();}...}

<块引用>

注意:任何代码都被发布到公共领域.无需署名.

Is there an Unload event, or any event, notification, message, mechanism, or hook, that i can use to be notified before the "default" application domain is unloaded?

i have code that needs to know when the application domain (almost always the default domain) is ending.

Note: i don't know what kind of application a developer will be creating when he uses my code. It could be:

  • a console application
  • a WinForms application
  • an ASP.net application
  • an ASP.net web-site
  • Runtime Callable Wrapper (RCW) COM object
  • a Windows Explorer shell extension
  • or a Windows Service

Either way, i need to know when the domain is closing, so i can do some "stuff". And i am not going to require the user to call any sort of "Shutdown" or "Cleanup" method. (Also, suggesting that the user be required to call a method themselves doesn't answer the question: which is about being notified when the app domain i'm running in is shut down).

See also

解决方案

i forgot to cross post my own answer from my other slight variation of this question. Ultimately, the answer came from an answer by M.A. Hanin.

There is no DomainUnload, but there is a ProcessExit:

class Contoso
{
   //constructor
   public Contoso()
   {
      //...

      //Catch domain shutdown (Hack: frantically look for things we can catch)
      if (AppDomain.CurrentDomain.IsDefaultAppDomain())
         AppDomain.CurrentDomain.ProcessExit += MyTerminationHandler;
      else
         AppDomain.CurrentDomain.DomainUnload += MyTerminationHandler;
   }

   private void MyTerminationHandler(object sender, EventArgs e)
   {
      //The domain is dying. Serialize out our values
      this.Dispose();
   }

   ...
}

Note: Any code is released into the public domain. No attribution required.

这篇关于默认应用程序域的卸载事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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