Question

Supose I have AppDomainA, which spins up AppDomainB. AppDomainB then spins up AppDomainC.

If, within AppDomainA I unload AppDomainB, does AppDomainC also get unloaded or must I make sure to handle that on my own?

Was it helpful?

Solution

Best way to find out is to try it. Here is an example of creating AppDomainA, which creates AppDomainB. We tell B to do some work, and unload A.

internal class Program
{
    private static Timer _timer;
    private static void Main(string[] args)
    {
        var domainA = AppDomain.CreateDomain("AppDomainA");
        domainA.DomainUnload += (s, e) => Console.WriteLine("DomainA was unloaded.");
        domainA.DoCallBack(() =>
        {
            var domainB = AppDomain.CreateDomain("AppDomainB");
            domainB.DomainUnload += (s, e) => Console.WriteLine("DomainB was unloaded.");
            domainB.DoCallBack(() =>
            {
                _timer = new Timer(o =>
                {
                    Console.WriteLine("Tick from AppDomain: " + AppDomain.CurrentDomain.FriendlyName);
                }, null, 0, 1000);
            });
        });
        AppDomain.Unload(domainA);
        Application.Run(); //Run a message loop so AppDomainB can keep doing work.
    }
}

We see we get the message AppDomainA was unloaded, but not B, and B keeps working. Our conclusion is you need to make sure to handle this on your own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top