This documentation is archived and is not being maintained.

AppDomain.Unload Method

Unloads the specified application domain.

[Visual Basic]
Public Shared Sub Unload( _
   ByVal domain As AppDomain _
)
[C#]
public static void Unload(
 AppDomain domain
);
[C++]
public: static void Unload(
 AppDomain* domain
);
[JScript]
public static function Unload(
   domain : AppDomain
);

Parameters

domain
An application domain to unload.

Exceptions

Exception Type Condition
ArgumentNullException domain is a null reference (Nothing in Visual Basic).
CannotUnloadAppDomainException domain could not be unloaded.

Remarks

A long period of time can pass before domain unloads because it might be difficult to terminate executing threads.

If the thread that invoked Unload is running in the domain, another thread is started to perform the unload operation. If domain cannot be unloaded, an instance of CannotUnloadAppDomainException is thrown to that thread, not the original thread that invoked Unload. However, if the thread that invoked Unload is running outside domain, that thread will receive the exception.

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

Example

[Visual Basic] 
Imports System
Imports System.Reflection
Imports System.Security.Policy 'for evidence object

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 'Main 
End Class 'ADUnload

[C#] 
using System;
using System.Reflection;
using System.Security.Policy;  //for evidence object
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.");
        }
        
    }
    
}

[C++] 
#using <mscorlib.dll>

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(S"MyDomain", adevidence);

   Console::WriteLine(S"Host domain: {0}", AppDomain::CurrentDomain->FriendlyName);
   Console::WriteLine(S"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(S"child domain: {0}", domain->FriendlyName);
   } catch (AppDomainUnloadedException* /*e*/) {
      Console::WriteLine(S"The appdomain MyDomain does not exist.");
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

AppDomain Class | AppDomain Members | System Namespace

Show: