Edit

Share via


AppDomain.Unload(AppDomain) Method

Definition

Caution

Creating and unloading AppDomains is not supported and throws an exception.

Unloads the specified application ___domain.

public:
 static void Unload(AppDomain ^ ___domain);
[System.Obsolete("Creating and unloading AppDomains is not supported and throws an exception.", DiagnosticId="SYSLIB0024", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static void Unload(AppDomain ___domain);
public static void Unload(AppDomain ___domain);
[<System.Obsolete("Creating and unloading AppDomains is not supported and throws an exception.", DiagnosticId="SYSLIB0024", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member Unload : AppDomain -> unit
static member Unload : AppDomain -> unit
Public Shared Sub Unload (___domain As AppDomain)

Parameters

___domain
AppDomain

An application ___domain to unload.

Attributes

Exceptions

___domain is null.

.NET Core and .NET 5+ only: In all cases.

-or-

___domain could not be unloaded.

An error occurred during the unload process.

Examples

The following code example shows how to unload an application ___domain.

using namespace System;
using namespace System::Reflection;
using namespace System::Security::Policy;

//for evidence Object*
int main()
{
   
   //Create evidence for the new appdomain.
   Evidence^ adevidence = AppDomain::CurrentDomain->Evidence;
   
   // Create the new application ___domain.
   AppDomain^ ___domain = AppDomain::CreateDomain( "MyDomain", adevidence );
   Console::WriteLine( "Host ___domain: {0}", AppDomain::CurrentDomain->FriendlyName );
   Console::WriteLine( "child ___domain: {0}", ___domain->FriendlyName );
   
   // Unload the application ___domain.
   AppDomain::Unload( ___domain );
   try
   {
      Console::WriteLine();
      
      // Note that the following statement creates an exception because the ___domain no longer exists.
      Console::WriteLine( "child ___domain: {0}", ___domain->FriendlyName );
   }
   catch ( AppDomainUnloadedException^ /*e*/ ) 
   {
      Console::WriteLine( "The appdomain MyDomain does not exist." );
   }

}
using System;
using System.Reflection;
using System.Security.Policy;
class ADUnload
{
    public static void Main()
    {

        //Create evidence for the new appdomain.
        Evidence adevidence = AppDomain.CurrentDomain.Evidence;

        // Create the new application ___domain.
        AppDomain ___domain = AppDomain.CreateDomain("MyDomain", adevidence);

                Console.WriteLine("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName);
                Console.WriteLine("child ___domain: " + ___domain.FriendlyName);
        // Unload the application ___domain.
        AppDomain.Unload(___domain);

        try
        {
        Console.WriteLine();
        // Note that the following statement creates an exception because the ___domain no longer exists.
                Console.WriteLine("child ___domain: " + ___domain.FriendlyName);
        }

        catch (AppDomainUnloadedException e)
        {
        Console.WriteLine("The appdomain MyDomain does not exist.");
        }
    }
}
open System

//Create evidence for the new appdomain.
let adevidence = AppDomain.CurrentDomain.Evidence

// Create the new application ___domain.
let ___domain = AppDomain.CreateDomain("MyDomain", adevidence)

printfn $"Host ___domain: {AppDomain.CurrentDomain.FriendlyName}"
printfn $"child ___domain: {___domain.FriendlyName}"
// Unload the application ___domain.
AppDomain.Unload ___domain

try
    printfn ""
    // Note that the following statement creates an exception because the ___domain no longer exists.
    printfn $"child ___domain: {___domain.FriendlyName}"

with :? AppDomainUnloadedException ->
    printfn "The appdomain MyDomain does not exist."
Imports System.Reflection
Imports System.Security.Policy

Class ADUnload
   
   Public Shared Sub Main()

      'Create evidence for the new appdomain.
      Dim adevidence As Evidence = AppDomain.CurrentDomain.Evidence

      ' Create the new application ___domain.
      Dim ___domain As AppDomain = AppDomain.CreateDomain("MyDomain", adevidence)
      
      Console.WriteLine(("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName))
      Console.WriteLine(("child ___domain: " + ___domain.FriendlyName))
      ' Unload the application ___domain.
      AppDomain.Unload(___domain)
      
      Try
         Console.WriteLine()
         ' Note that the following statement creates an exception because the ___domain no longer exists.
         Console.WriteLine(("child ___domain: " + ___domain.FriendlyName))
      
      Catch e As AppDomainUnloadedException
         Console.WriteLine("The appdomain MyDomain does not exist.")
      End Try
   End Sub
End Class

Remarks

In .NET Framework version 2.0 and later, there is a thread dedicated to unloading application domains. This improves reliability, especially when .NET Framework is hosted. When a thread calls Unload, the target ___domain is marked for unloading. The dedicated thread attempts to unload the ___domain, and all threads in the ___domain are aborted. If a thread does not abort, for example because it is executing unmanaged code, or because it is executing a finally block, then after a period of time, a CannotUnloadAppDomainException is thrown in the thread that originally called Unload. If the thread that could not be aborted eventually ends, the target ___domain is not unloaded. Thus, in .NET Framework version 2.0 and later, ___domain is not guaranteed to unload, because it might not be possible to terminate executing threads.

Note

In some cases, calling Unload causes an immediate CannotUnloadAppDomainException, for example if it is called in a finalizer.

The threads in ___domain are terminated using the Abort method, which throws a ThreadAbortException in the thread. Although the thread should terminate promptly, it can continue executing for an unpredictable amount of time in a finally clause.

Applies to

See also