简体   繁体   English

卸载默认应用程序域的事件?

[英]Unload event for the default Application Domain?

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 一个WinForms应用程序
  • an ASP.net application 一个ASP.net应用程序
  • an ASP.net web-site 一个ASP.net网站
  • Runtime Callable Wrapper (RCW) COM object 运行时可调用包装器(RCW)COM对象
  • a Windows Explorer shell extension Windows资源管理器外壳扩展
  • or a Windows Service 或Windows服务

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 MA Hanin . 最终,答案来自MA Hanin的回答

There is no DomainUnload , but there is a ProcessExit : 没有DomainUnload ,但有一个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. 无需归属。

AppDomain.CurrentDomain.DomainUnload += 
    (object sender, EventArgs e) => { /*do stuff*/ };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM