Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
AppDomain Class
AppDomain Methods
 Unload Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.Net Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
AppDomain..::.Unload Method

Unloads the specified application domain.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<SecurityPermissionAttribute(SecurityAction.Demand, ControlAppDomain := True)> _
Public Shared Sub Unload ( _
    domain As AppDomain _
)
Visual Basic (Usage)
Dim domain As AppDomain

AppDomain.Unload(domain)
C#
[SecurityPermissionAttribute(SecurityAction.Demand, ControlAppDomain = true)]
public static void Unload(
    AppDomain domain
)
Visual C++
[SecurityPermissionAttribute(SecurityAction::Demand, ControlAppDomain = true)]
public:
static void Unload(
    AppDomain^ domain
)
JScript
public static function Unload(
    domain : AppDomain
)

Parameters

domain
Type: System..::.AppDomain
An application domain to unload.
ExceptionCondition
ArgumentNullException

domain is nullNothingnullptra null reference (Nothing in Visual Basic).

CannotUnloadAppDomainException

domain could not be unloaded.

Exception

An error occurred during the unload process.

In the .NET Framework version 2.0 there is a thread dedicated to unloading application domains. This improves reliability, especially when the .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 the .NET Framework version 2.0 domain is not guaranteed to unload, because it might not be possible to terminate executing threads.

NoteNote:

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.

Version Compatibility

In the .NET Framework version 1.0 and 1.1 if the thread that calls Unload is running in domain, another thread is started to perform the unload operation. If domain cannot be unloaded, a CannotUnloadAppDomainException is thrown in that thread, not in the original thread that called Unload. However, if the thread that calls Unload is running outside domain, that thread receives the exception.

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

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.");
        }
        
    }
    
}

Visual C++
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." );
   }

}


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Timeout      Sunghwa Jin ... Thomas Lee   |   Edit   |   Show History

Here is detailed timeout story excerpted from Jeffrey Richter's CLR via C# book (I got confirmation from him that it is ok to post this content here):

By the way, when a thread calls AppDomain.Unload, the CLR waits 10 seconds (by default) for the threads in the unloading AppDomain to leave it. If after 10 seconds, the thread that called AppDomain.Unload doesn’t return, it will throw a CannotUnloadAppDomainException, and the AppDomain may or may not be unloaded in the future.

Note If a thread calling AppDomain.Unload is in the AppDomain being unloaded, the CLR creates another thread, and this new thread attempts to unload the AppDomain. The first thread will forcibly throw the ThreadAbortException and unwind. The new thread will waitfor the AppDomain to unload, and then the new thread terminates. If the AppDomain fails to unload, the new thread will process a CannotUnloadAppDomainException, but since you did not write the code that this new thread executes, you can’t catch this exception.

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker