AppDomain.CurrentDomain.DomainUnload will not load in the console application

I have an assembly in which, upon access, a single thread is created to process the elements placed in the queue. In this assembly, I attach a DomainUnload event handler:

AppDomain.CurrentDomain.DomainUnload += new EventHandler(CurrentDomain_DomainUnload);

This handler attaches the thread to the main thread so that all elements in the queue can complete processing before the application expires.

The problem I encountered is that the DomainUnload event does not fire when the console application terminates. Any ideas why this would be?

Using .NET 3.5 and C #

+4
source share
2 answers

Unfortunately for you this event does not occur in AppDomain by default, only in application domains created within the default limits.

From the MSDN documentation :

This event never occurs in the default application domain.

+12
source

You will need to sign up for an event for a specific domain. You also cannot expect the domain to be unloaded at the time of completion. Remove the comment from this code to see that:

 using System; using System.Reflection; class Program { static void Main(string[] args) { var ad = AppDomain.CreateDomain("test"); ad.DomainUnload += ad_DomainUnload; //AppDomain.Unload(ad); Console.ReadLine(); } static void ad_DomainUnload(object sender, EventArgs e) { Console.WriteLine("unloaded, press Enter"); Console.ReadLine(); } } 
+1
source

Source: https://habr.com/ru/post/1307709/


All Articles