Unload event for the default Application Domain?

后端 未结 2 1933
失恋的感觉
失恋的感觉 2020-12-11 10:31

Is there an Unload event, or any event, notification, message, mechanism, or hook, that i can use to be notified before the \"default\" applicatio

相关标签:
2条回答
  • 2020-12-11 10:38

    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.

    0 讨论(0)
  • 2020-12-11 10:56
    AppDomain.CurrentDomain.DomainUnload += 
        (object sender, EventArgs e) => { /*do stuff*/ };
    
    0 讨论(0)
提交回复
热议问题