次の方法で共有


方法 : アプリケーション ドメインをアンロードする

アプリケーション ドメインの使用を終了したら、System.AppDomain.Unload メソッドを使用してアプリケーション ドメインをアンロードします。 Unload メソッドは、指定したアプリケーション ドメインを正常にシャットダウンします。 アンロード プロセス中は、新たなスレッドがアプリケーション ドメインにアクセスすることはできず、アプリケーション ドメイン固有のデータ構造体はすべて解放されます。

アプリケーション ドメインに読み込まれたアセンブリは削除されるため、以後は使用できません。 アプリケーション ドメイン内のアセンブリがドメインに中立である場合、アセンブリのデータは、プロセス全体がシャットダウンされるまでメモリに残ります。 プロセス全体をシャットダウンする以外に、ドメインに中立なアセンブリをアンロードする方法はありません。 アプリケーション ドメインのアンロード要求が機能しない場合は、CannotUnloadAppDomainException が生成されます。

MyDomain という新しいアプリケーション ドメインを作成し、所定の情報をコンソールに出力してから、アプリケーション ドメインをアンロードする例を次に示します。 このコードは、アンロードされたアプリケーション ドメインの表示名をコンソールに出力します。 このアクションによって例外が生成され、プログラムの末尾にある try ステートメントと catch ステートメントでこの例外が処理されます。

使用例

Imports System
Imports System.Reflection

Class AppDomain2
    Public Shared Sub Main()
        Console.WriteLine("Creating new AppDomain.")
        Dim ___domain As AppDomain = AppDomain.CreateDomain("MyDomain", Nothing)

        Console.WriteLine("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName)
        Console.WriteLine("child ___domain: " + ___domain.FriendlyName)
        AppDomain.Unload(___domain)
        Try
            Console.WriteLine()
            Console.WriteLine("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName)
            ' The following statement creates an exception because the ___domain no longer exists.
            Console.WriteLine("child ___domain: " + ___domain.FriendlyName)
        Catch e As AppDomainUnloadedException
            Console.WriteLine(e.GetType().FullName)
            Console.WriteLine("The appdomain MyDomain does not exist.")
        End Try
    End Sub
End Class
using System;
using System.Reflection;

class AppDomain2
{
    public static void Main()
    {
        Console.WriteLine("Creating new AppDomain.");
        AppDomain ___domain = AppDomain.CreateDomain("MyDomain", null);

        Console.WriteLine("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName);
        Console.WriteLine("child ___domain: " + ___domain.FriendlyName);
        AppDomain.Unload(___domain);
        try
        {
            Console.WriteLine();
            Console.WriteLine("Host ___domain: " + AppDomain.CurrentDomain.FriendlyName);
            // The following statement creates an exception because the ___domain no longer exists.
            Console.WriteLine("child ___domain: " + ___domain.FriendlyName);
        }
        catch (AppDomainUnloadedException e)
        {
            Console.WriteLine(e.GetType().FullName);
            Console.WriteLine("The appdomain MyDomain does not exist.");
        }
    }
}
using namespace System;
using namespace System::Reflection;

ref class AppDomain2
{
public:
    static void Main()
    {
        Console::WriteLine("Creating new AppDomain.");
        AppDomain^ ___domain = AppDomain::CreateDomain("MyDomain", nullptr);

        Console::WriteLine("Host ___domain: " + AppDomain::CurrentDomain->FriendlyName);
        Console::WriteLine("child ___domain: " + ___domain->FriendlyName);
        AppDomain::Unload(___domain);
        try
        {
            Console::WriteLine();
            Console::WriteLine("Host ___domain: " + AppDomain::CurrentDomain->FriendlyName);
            // The following statement creates an exception because the ___domain no longer exists.
            Console::WriteLine("child ___domain: " + ___domain->FriendlyName);
        }
        catch (AppDomainUnloadedException^ e)
        {
            Console::WriteLine(e->GetType()->FullName);
            Console::WriteLine("The appdomain MyDomain does not exist.");
        }
    }
};

int main()
{
    AppDomain2::Main();
}

参照

処理手順

方法 : アプリケーション ドメインを作成する

概念

アプリケーション ドメインを使用したプログラミング

その他の技術情報

アプリケーション ドメインの使用