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 會在原本呼叫的執行緒中擲回 UnloadIf 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 ,而此方法會 ThreadAbortException 線上程中擲回。The threads in domain are terminated using the Abort method, which throws a ThreadAbortException in the thread. 雖然執行緒應該會立即終止,但它可以在子句中繼續執行達無法預期的時間量 finallyAlthough 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 版中,如果呼叫的執行緒正在 Unloaddomain 執行,則會啟動另一個執行緒來執行卸載作業。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 則會在該執行緒中擲回,而不是在呼叫的原始執行緒中擲回 UnloadIf 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.

適用於