AppDomain.Unload(AppDomain) メソッド

定義

注意事項

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

アンロードするアプリケーション ドメイン。

属性

例外

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 がホストされている場合に、信頼性が向上します。 スレッドが Unloadを呼び出すと、ターゲット ドメインはアンロード対象としてマークされます。 専用スレッドはドメインのアンロードを試み、ドメイン内のすべてのスレッドは中止されます。 アンマネージド コードを実行している場合や、finally ブロックを実行しているためなど、スレッドが中止されない場合は、一定期間後に、CannotUnloadAppDomainExceptionを最初に呼び出したスレッドでUnloadがスローされます。 中止できなかったスレッドが最終的に終了した場合、ターゲット ドメインはアンロードされません。 したがって、 domain は、実行中のスレッドを終了できない可能性があるため、アンロードは保証されません。

Note

場合によっては、 Unload を呼び出すと、ファイナライザーで呼び出された場合など、すぐに CannotUnloadAppDomainExceptionが発生します。

domain内のスレッドは、スレッドでAbortをスローするThreadAbortException メソッドを使用して終了されます。 スレッドは直ちに終了する必要がありますが、 finally 句で予期しない時間実行を続けることができます。

適用対象

こちらもご覧ください