System.AppDomain.Unload Method

Unloads the specified application domain.

Syntax

[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.MayCorruptAppDomain, System.Runtime.ConstrainedExecution.Cer.MayFail)]
public static void Unload (AppDomain domain)

Parameters

domain
An application domain to unload.

Exceptions

TypeReason
ArgumentNullException domain is null.
CannotUnloadAppDomainException domain could not be unloaded.

Remarks

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 AppDomain.Unload(AppDomain), 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 AppDomain.Unload(AppDomain). 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.

Note:

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

The threads in domain are terminated using the System.Threading.Thread.Abort(object) method, which throws a System.Threading.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 AppDomain.Unload(AppDomain) 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 AppDomain.Unload(AppDomain). However, if the thread that calls AppDomain.Unload(AppDomain) is running outside domain, that thread receives the exception.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0