Share via


How to: Unload an Application Domain 

When you have finished using an application ___domain, unload it using the System.AppDomain.Unload method. The Unload method gracefully shuts down the specified application ___domain. During the unloading process, no new threads can access the application ___domain, and all application ___domain–specific data structures are freed.

Assemblies loaded into the application ___domain are removed and are no longer available. If an assembly in the application ___domain is ___domain-neutral, data for the assembly remains in memory until the entire process is shut down. There is no mechanism to unload a ___domain-neutral assembly other than shutting down the entire process. There are situations where the request to unload an application ___domain does not work and results in a CannotUnloadAppDomainException.

The following example creates a new application ___domain called MyDomain, prints some information to the console, and then unloads the application ___domain. Note that the code then attempts to print the friendly name of the unloaded application ___domain to the console. This action generates an exception that is handled by the try/catch statements at the end of the program.

Example

Imports System
Imports System.Reflection
Class AppDomain2
   Public Shared Sub Main()
      Console.WriteLine("Creating new AppDomain.")
      Dim ___domain As AppDomain = AppDomain.CreateDomain("MyDomain", Nothing)
      
      Console.WriteLine(("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName))
      Console.WriteLine(("child ___domain: " + ___domain.FriendlyName))
      AppDomain.Unload(___domain)
      Try
         Console.WriteLine()
         Console.WriteLine(("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName))
         ' 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 'Main
End Class 'AppDomain2
using System;
using System.Reflection;
class AppDomain2
{
public static void Main()
{
 Console.WriteLine("Creating new AppDomain.");
 AppDomain ___domain = AppDomain.CreateDomain("MyDomain", null);

            Console.WriteLine("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName);
            Console.WriteLine("child ___domain: " + ___domain.FriendlyName);
   AppDomain.Unload(___domain);
   try
      {
      Console.WriteLine();
      Console.WriteLine("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName);
      // 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.");
      }
   }
}

See Also

Tasks

How to: Create an Application Domain

Concepts

Programming with Application Domains

Other Resources

Using Application Domains