Export (0) Print
Expand All

AppDomain.Unload Method

Unloads the specified application domain.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static void Unload (
	AppDomain domain
)
public static void Unload (
	AppDomain domain
)
public static function Unload (
	domain : AppDomain
)

Parameters

domain

An application domain to unload.

Exception typeCondition

ArgumentNullException

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

CannotUnloadAppDomainException

domain could not be unloaded.

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.

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

import System.*;
import System.Reflection.*;
import System.Security.Policy.*; //for evidence object

class ADUnload
{
    public static void main(String[] args)
    {
        // Create evidence for the new appdomain.
        Evidence adEvidence = AppDomain.get_CurrentDomain().get_Evidence();

        // Create the new application domain.
        AppDomain domain = AppDomain.CreateDomain("MyDomain", adEvidence);

        Console.WriteLine("Host domain: " 
            + AppDomain.get_CurrentDomain().get_FriendlyName());
        Console.WriteLine("child domain: " + domain.get_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.get_FriendlyName());
        }
        catch (AppDomainUnloadedException e) {
            Console.WriteLine("The appdomain MyDomain does not exist.");
        }
    } //main
} //ADUnload

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft