Unloads the specified application domain.
Assembly: mscorlib (in mscorlib.dll)
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 , 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 . 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.
In some cases, calling 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.
In the .NET Framework version 1.0 and 1.1 if the thread that calls 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 . However, if the thread that calls is running outside domain, that thread receives the exception.
The following code example shows how to unload an application domain.
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
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2