AppDomain.Unload(AppDomain) 方法

定義

卸載指定的應用程式定義域。Unloads the specified application domain.

public:
 static void Unload(AppDomain ^ domain);
public static void Unload (AppDomain domain);
static member Unload : AppDomain -> unit
Public Shared Sub Unload (domain As AppDomain)

參數

domain
AppDomain

要卸載的應用程式定義域。An application domain to unload.

例外狀況

domainnulldomain is null.

無法卸載 domaindomain could not be unloaded.

卸載程序期間發生錯誤。An error occurred during the unload process.

範例

下列程式碼範例顯示如何卸載應用程式域。The following code example shows how to unload an application domain.

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

}

using System;
using System.Reflection;
using System.Security.Policy;
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.");
        }
    }
}
Imports System.Reflection
Imports System.Security.Policy

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
End Class

備註

在 .NET Framework 版本2.0 中,有一個專門用來卸載應用程式域的執行緒。In the .NET Framework version 2.0 there is a thread dedicated to unloading application domains. 這可改善可靠性,特別是在裝載 .NET Framework 時。This improves reliability, especially when the .NET Framework is hosted. 當執行緒呼叫 Unload時,目標網域會標示為要卸載。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. 如果執行緒不會中止(例如因為它正在執行非受控程式碼),或因為它正在執行 finally 區塊,則在一段時間之後,CannotUnloadAppDomainException 會在原本呼叫 Unload的執行緒中擲回。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. 因此,在 .NET Framework 版本 2.0 domain 不保證會卸載,因為它可能無法終止執行中的執行緒。Thus, in the .NET Framework version 2.0 domain is not guaranteed to unload, because it might not be possible to terminate executing threads.

注意

在某些情況下,呼叫 Unload 會造成立即 CannotUnloadAppDomainException,例如,如果在完成項中呼叫它。In some cases, calling Unload causes an immediate CannotUnloadAppDomainException, for example if it is called in a finalizer.

domain 中的執行緒會使用 Abort 方法來終止,這會線上程中擲回 ThreadAbortExceptionThe threads in domain are terminated using the Abort method, which throws a ThreadAbortException in the thread. 雖然執行緒應該會立即終止,但是它可以繼續在 finally 子句中執行一段無法預期的時間。Although the thread should terminate promptly, it can continue executing for an unpredictable amount of time in a finally clause.

版本相容性Version Compatibility

在 .NET Framework 版本1.0 和1.1 中,如果呼叫 Unload 的執行緒正在 domain中執行,則會啟動另一個執行緒來執行卸載作業。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. 如果無法卸載 domain,就會在該執行緒中擲回 CannotUnloadAppDomainException,而不是在呼叫 Unload的原始執行緒中。If domain cannot be unloaded, a CannotUnloadAppDomainException is thrown in that thread, not in the original thread that called Unload. 不過,如果呼叫 Unload 的執行緒在 domain外部執行,該執行緒就會收到例外狀況。However, if the thread that calls Unload is running outside domain, that thread receives the exception.

適用於