AppDomain.Unload(AppDomain) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
警告
Creating and unloading AppDomains is not supported and throws an exception.
卸載指定的應用程式域。
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);
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}")>]
static member Unload : AppDomain -> unit
static member Unload : AppDomain -> unit
Public Shared Sub Unload (domain As AppDomain)
參數
- domain
- AppDomain
一個可以卸載的應用程式領域。
- 屬性
例外狀況
domain 是 null。
卸載過程中發生錯誤。
範例
以下程式碼範例說明如何卸載應用程式域。
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 時。 當執行緒呼叫 Unload時,目標網域會被標記為卸載。 專用執行緒嘗試卸載網域,但該域內的所有執行緒都會被中止。 如果執行緒沒有中止,例如因為它正在執行未受管理的程式碼,或是執行 finally 一個區塊,那麼在一段時間後, CannotUnloadAppDomainException 會被丟入原本呼叫 Unload的執行緒中。 如果無法中止的執行緒最終結束,目標網域則不會被卸載。 因此, domain 不保證會卸載,因為執行執行緒可能無法終止。
Note
在某些情況下,呼叫 Unload 會立即產生 CannotUnloadAppDomainException,例如在終結子中被呼叫。
線 domain 程會用 方法 Abort 終止,並丟 ThreadAbortException 入執行緒。 雖然執行緒應該會立即終止,但它在某 finally 個子句中可能會持續執行一段時間,時間不可預測。