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

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

问题描述

是否有一个的卸载的事件,或的事件,通知,信息,机械,或钩,我可以用它来进行之前的默认的通知的应用程序域被卸载?



我有需要时,应用程序域(几乎总是默认域)已到尽头就知道代码。



请注意:我不知道他什么时候用我的代码开发人员将创建什么样的应用。这可能是:




  • 一个控制台应用程序

  • WinForms应用程序

  • 的ASP.net应用程序

  • 的ASP.net网站

  • 运行时可调用包装(RCW)COM对象

  • Windows资源管理器外壳扩展

  • 或Windows服务



无论哪种方式,我需要在域被关闭知道,这样我就可以做一些的东西的。而且我不会要求用户拨打任何形式的关机清理的方法。 (另外,提示用户需要调用方法本身不回答这个问题:这是什么时候我跑的应用程序域关闭时被通知)。



又见




解决方案

我忘了后我自己的答案跨越从这个问题我的其他轻微变化。最终,答案来自由MA HANIN 的答案。



没有 DomainUnload ,但有一个 ProcessExit

 类的Contoso 
{
//构造
公共Contoso的()
{
// ...

//捕捉域关机(哈克:疯狂地找东西,我们可以赶上)
如果(AppDomain.CurrentDomain.IsDefaultAppDomain())
AppDomain.CurrentDomain.ProcessExit + = MyTerminationHandler;
,否则
AppDomain.CurrentDomain.DomainUnload + = MyTerminationHandler;
}

私人无效MyTerminationHandler(对象发件人,EventArgs五)
{
//域名正在消亡。序列化了我们的价值
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天全站免登陆