Description



Description

CannotUnloadAppDomainException is thrown when there is an attempt to unload:

  • The default application domain, which must remain loaded during the lifetime of the application.

  • An application domain with a running thread that cannot immediately stop execution.

  • An application domain that has already been unloaded.

Example

using System;

using System.Threading;



/// <summary>

/// Shows why a CannotUnloadAppDomainException is thrown

/// and how to handle it

/// </summary>



public class CannotUnloadAppDomainExceptionSample

{

    public static void Main()

    {

        try

        {

            AppDomain ad = Thread.GetDomain();

            AppDomain.Unload(ad);

        }

        catch (CannotUnloadAppDomainException e)

        {

            Console.WriteLine("Caught exception: {0} - '{1}'", e.GetType().Name, e.Message);

        }

        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue"); Console.ReadLine();

    }

}


The output is


Caught exception: CannotUnloadAppDomainException - 'The default domain can not be unloaded.'





Press Enter to continue