AppDomain.Unload(AppDomain) Metoda

Definicja

Przestroga

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

Zwalnia określoną domenę aplikacji.

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)

Parametry

domain
AppDomain

Domena aplikacji do zwolnienia.

Atrybuty

Wyjątki

domain to null.

Tylko platformy .NET Core i .NET 5+: we wszystkich przypadkach.

-lub-

domain nie można zwolnić.

Wystąpił błąd podczas procesu zwalniania.

Przykłady

W poniższym przykładzie kodu pokazano, jak zwolnić domenę aplikacji.

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

Uwagi

W .NET Framework wersji 2.0 lub nowszej istnieje wątek przeznaczony do zwalniania domen aplikacji. Zwiększa to niezawodność, szczególnie w przypadku hostów .NET Framework. Po wywołaniu Unloadwątku domena docelowa jest oznaczona do zwalniania. Dedykowany wątek próbuje zwolnić domenę, a wszystkie wątki w domenie zostaną przerwane. Jeśli wątek nie przerywa się, na przykład dlatego, że wykonuje niezarządzany kod lub ponieważ wykonuje finally blok, to po upływie czasu element jest zgłaszany w wątku, CannotUnloadAppDomainException który pierwotnie nazywa się Unload. Jeśli wątek, którego nie można przerwać, kończy się ostatecznie, domena docelowa nie zostanie zwolniona. W związku z tym w wersji .NET Framework w wersji 2.0 i nowszej nie ma gwarancji zwolnienia, domain ponieważ może nie być możliwe zakończenie wykonywania wątków.

Uwaga

W niektórych przypadkach wywołanie powoduje natychmiastowe CannotUnloadAppDomainExceptionwywołanie Unload metody , na przykład jeśli jest wywoływane w finalizatorze.

Wątki w pliku domain są przerywane przy użyciu Abort metody , która zgłasza wątek ThreadAbortException . Mimo że wątek powinien zostać natychmiast zakończony, może on kontynuować wykonywanie przez nieprzewidywalny czas w klauzuli finally .

Dotyczy

Zobacz też