4

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

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • So you are building a library that would be consumed by other application and you want to know when the default app domain of those applications is unloaded ? – Ibrahim Najjar Aug 03 '13 at 13:42
  • Enumerating AppDomains is not possible. – Hans Passant Aug 03 '13 at 14:58
  • @Sniffer i want to know when the AppDomain that i am in (which 100% of the time will be the default domain) is being unloaded, so i can *"shutdown"*. – Ian Boyd Aug 06 '13 at 13:39
  • You could put your cleanup logic in a [critical finalizer object](http://msdn.microsoft.com/en-us/library/system.runtime.constrainedexecution.criticalfinalizerobject.aspx). That would be sort of an abuse though. Can't you go the other way around: don't cleanup on shutdown but on initializing? If you find there are left overs from a previous run, clean them up, if required. (i.e. "design for crash"). Leave the rest to the OS. Exiting the default-domain will exit the process anyway (not sure about hosted CLRs though). – Christian.K Aug 07 '13 at 09:28
  • @Christian.K The issue with *"cleanup"* isn't to release resources. i need to perform *tasks* (e.g. flushing items to the filing system, saving statistics to SQL Server, uploading persistent state to a web-server). And i want to do it without the user having to call some sort of `Shutdown()` method (like i don't have to do in other languages). – Ian Boyd Aug 07 '13 at 17:00
  • 1
    OK, I understand. Have you looked at [AppDomain.ProcessExit](http://msdn.microsoft.com/en-us/library/system.appdomain.processexit.aspx). Conceptually that event should also signal the end of the default-AppDomain (and is also raised in the default domain). The total execution time of suche event handlers is limited, however, and the work you try to do sounds rather "involved". YMMV. – Christian.K Aug 07 '13 at 17:16
  • http://stackoverflow.com/questions/16673332/net-code-execution-at-normal-process-exit/16678779#16678779 might provide some hints – M.A. Hanin Aug 12 '13 at 20:59
  • 1
    It should be noted that AppDomain.ProcessExit won't fire if the process is terminated through, for example, the Task Manager. – Cole Campbell Sep 06 '13 at 14:47

2 Answers2

8

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.

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • 1
    There is actually a [DomainUnload](http://msdn.microsoft.com/en-us/library/system.appdomain.domainunload%28v=vs.110%29.aspx) event. Just wanted to clarify that, though the way that you did this is actually better. – Chuck Dee Dec 01 '14 at 22:03
4
AppDomain.CurrentDomain.DomainUnload += 
    (object sender, EventArgs e) => { /*do stuff*/ };
N-ate
  • 6,051
  • 2
  • 40
  • 48