AppDomain.DomainUnload never fired automatically
-
Hi! In my app I'd like to execute some code when an AppDomain is unloaded. I expected AppDomain.DomainUnload to be suitable for this, however the event never gets raised automatically - only if I call AppDomain.Unload with that AppDomain. Example:
class Class1
{
static void Main(string[] args)
{
// subscribe to current domain
AppDomain.CurrentDomain.DomainUnload += new EventHandler(CurrentDomain_DomainUnload);// and for testing to another domain: AppDomain domain = AppDomain.CreateDomain("Test"); domain.DomainUnload += new EventHandler(CurrentDomain\_DomainUnload); // if I uncomment this, the event is fired for 'domain': //AppDomain.Unload(domain); } public static void CurrentDomain\_DomainUnload(object sender, EventArgs e) { Console.WriteLine("Unloading appdomain"); AppDomain domain = sender as AppDomain; if(domain != null) Console.WriteLine("... unloading domain is: " + domain.FriendlyName); }
}
I find this quite irritating. How can I implement some kind of per-AppDomain shutdown function? Thanks in advance. Regards, Andre Loker
-
Hi! In my app I'd like to execute some code when an AppDomain is unloaded. I expected AppDomain.DomainUnload to be suitable for this, however the event never gets raised automatically - only if I call AppDomain.Unload with that AppDomain. Example:
class Class1
{
static void Main(string[] args)
{
// subscribe to current domain
AppDomain.CurrentDomain.DomainUnload += new EventHandler(CurrentDomain_DomainUnload);// and for testing to another domain: AppDomain domain = AppDomain.CreateDomain("Test"); domain.DomainUnload += new EventHandler(CurrentDomain\_DomainUnload); // if I uncomment this, the event is fired for 'domain': //AppDomain.Unload(domain); } public static void CurrentDomain\_DomainUnload(object sender, EventArgs e) { Console.WriteLine("Unloading appdomain"); AppDomain domain = sender as AppDomain; if(domain != null) Console.WriteLine("... unloading domain is: " + domain.FriendlyName); }
}
I find this quite irritating. How can I implement some kind of per-AppDomain shutdown function? Thanks in advance. Regards, Andre Loker
What about the ProcessExited event?
-
What about the ProcessExited event?