共用方式為


AppDomain.Unload(AppDomain) 方法

定義

警告

Creating and unloading AppDomains is not supported and throws an exception.

卸載指定的應用程式定義域。

public:
 static void Unload(AppDomain ^ domain);
public static void Unload (AppDomain domain);
[System.Obsolete("Creating and unloading AppDomains is not supported and throws an exception.", DiagnosticId="SYSLIB0024", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static void Unload (AppDomain domain);
static member Unload : AppDomain -> unit
[<System.Obsolete("Creating and unloading AppDomains is not supported and throws an exception.", DiagnosticId="SYSLIB0024", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member Unload : AppDomain -> unit
Public Shared Sub Unload (domain As AppDomain)

參數

domain
AppDomain

要卸載的應用程式定義域。

屬性

例外狀況

domainnull

僅限 .NET Core 與 .NET 5+:在所有情況下。

-或-

無法卸載 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.");
        }
    }
}
open System

//Create evidence for the new appdomain.
let adevidence = AppDomain.CurrentDomain.Evidence

// Create the new application domain.
let domain = AppDomain.CreateDomain("MyDomain", adevidence)

printfn $"Host domain: {AppDomain.CurrentDomain.FriendlyName}"
printfn $"child domain: {domain.FriendlyName}"
// Unload the application domain.
AppDomain.Unload domain

try
    printfn ""
    // Note that the following statement creates an exception because the domain no longer exists.
    printfn $"child domain: {domain.FriendlyName}"

with :? AppDomainUnloadedException ->
    printfn "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 版和更新版本中,有一個專用於卸除應用程式域的線程。 這可改善可靠性,特別是在裝載 .NET Framework 時。 當線程呼叫 Unload時,目標網域會標示為卸除。 專用線程會嘗試卸除網域,且網域中的所有線程都會中止。 如果線程未中止,例如因為它正在執行 Unmanaged 程式代碼,或因為它正在執行 finally 區塊,然後在一段時間之後, CannotUnloadAppDomainException 會在原本稱為 Unload的線程中擲回 。 如果最終無法中止的線程結束,則不會卸除目標網域。 因此,在 .NET Framework 2.0 版和更新版本中,domain不保證卸除,因為可能無法終止執行中的線程。

注意

在某些情況下,呼叫 Unload 會導致立即 CannotUnloadAppDomainException呼叫 ,例如,如果在完成項中呼叫它。

中的 domain 線程會使用 Abort 方法終止,該方法會在線程中擲回 ThreadAbortException 。 雖然線程應該會立即終止,但它可以在 子句中 finally 繼續執行無法預測的時間量。

適用於

另請參閱