簡體   English   中英

卸載默認應用程序域的事件?

[英]Unload event for the default Application Domain?

是否有卸載事件,或任何事件,通知,消息,機制或掛鈎,我可以用來在“默認”應用程序域卸載之前得到通知?

我有代碼需要知道什么時候應用程序域(幾乎總是默認域)結束。

注意:我不知道開發人員在使用我的代碼時會創建什么樣的應用程序。 它可能是:

  • 控制台應用程序
  • 一個WinForms應用程序
  • 一個ASP.net應用程序
  • 一個ASP.net網站
  • 運行時可調用包裝器(RCW)COM對象
  • Windows資源管理器外殼擴展
  • 或Windows服務

無論哪種方式,我需要知道域何時關閉,所以我可以做一些“東西” 而且我不會要求用戶調用任何類型的“關閉”“清理”方法。 (另外,建議用戶需要自己調用方法不回答問題:當我正在運行的應用程序域關閉時,這是關於通知的)。

也可以看看

我忘了從我對這個問題的其他微小變化中反復發表我自己的答案。 最終,答案來自MA Hanin的回答

沒有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();
   }

   ...
}

注意 :任何代碼都將發布到公共域中。 無需歸屬。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM