AppDomain.Unload 方法

卸载指定的应用程序域。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Sub Unload ( _
    domain As AppDomain _
)
用法
Dim domain As AppDomain

AppDomain.Unload(domain)
public static void Unload (
    AppDomain domain
)
public:
static void Unload (
    AppDomain^ domain
)
public static void Unload (
    AppDomain domain
)
public static function Unload (
    domain : AppDomain
)

参数

  • domain
    要卸载的应用程序域。

异常

异常类型 条件

ArgumentNullException

domain 为 空引用(在 Visual Basic 中为 Nothing)。

CannotUnloadAppDomainException

domain 未能卸载。

备注

在 .NET Framework 版本 2.0 中,有一个专用于卸载应用程序域的线程。这样可提高可靠性,尤其在 .NET Framework 已承载的情况下。当线程调用 Unload 时,将标记用于卸载的目标域。专用线程尝试卸载域,域中所有线程都中止。如果某个线程不中止,例如,因为其正在执行非托管代码或正在执行 finally 块,则一段时间后,原始调用 Unload 的线程中将引发 CannotUnloadAppDomainException。如果无法中止的线程最终结束,则目标域没有完成卸载。因此,.NET Framework 版本 2.0 中 domain 不能保证卸载,因为其可能无法终止正在执行的线程。

提示

在某些情况下,调用 Unload 将即刻引发 CannotUnloadAppDomainException,例如在终结器中调用时。

domain 中的线程使用 Abort 方法终止,此方法在线程中引发 ThreadAbortException。尽管线程应立刻终止,但是它可能在 finally 子句中继续执行一段时间,而且时间长短无法预计。

版本兼容性

在 .NET Framework 版本 1.0 和 1.1 中,如果调用 Unload 的线程在 domain 中运行,则将启动另一个线程执行卸载操作。如果无法卸载 domain,则在该线程中引发 CannotUnloadAppDomainException,而不是在调用 Unload 的原始线程中引发。但是,如果调用 Unload 的线程正在 domain 外运行,则该线程将会收到该异常。

示例

Imports System
Imports System.Reflection
Imports System.Security.Policy 'for evidence object

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 'Main 
End Class 'ADUnload
using System;
using System.Reflection;
using System.Security.Policy;  //for evidence object
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.");
        }
        
    }
    
}
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." );
   }

}
import System.*;
import System.Reflection.*;
import System.Security.Policy.*; //for evidence object

class ADUnload
{
    public static void main(String[] args)
    {
        // Create evidence for the new appdomain.
        Evidence adEvidence = AppDomain.get_CurrentDomain().get_Evidence();

        // Create the new application domain.
        AppDomain domain = AppDomain.CreateDomain("MyDomain", adEvidence);

        Console.WriteLine("Host domain: " 
            + AppDomain.get_CurrentDomain().get_FriendlyName());
        Console.WriteLine("child domain: " + domain.get_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.get_FriendlyName());
        }
        catch (AppDomainUnloadedException e) {
            Console.WriteLine("The appdomain MyDomain does not exist.");
        }
    } //main
} //ADUnload

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

AppDomain 类
AppDomain 成员
System 命名空间