AppDomainUnloadedException-Klasse

Die Ausnahme, die beim Zugriff auf eine entladene Anwendungsdomäne ausgelöst wird.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class AppDomainUnloadedException
    Inherits SystemException
'Usage
Dim instance As AppDomainUnloadedException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class AppDomainUnloadedException : SystemException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class AppDomainUnloadedException : public SystemException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class AppDomainUnloadedException extends SystemException
SerializableAttribute 
ComVisibleAttribute(true) 
public class AppDomainUnloadedException extends SystemException

Hinweise

In .NET Framework, Version 2.0 hat eine AppDomainUnloadedException, die nicht im Benutzercode verarbeitet wird, folgende Auswirkung:

  • Wenn ein Thread in verwaltetem Code gestartet wurde, wird er beendet. Die unbehandelte Ausnahme darf die Anwendung nicht beenden.

  • Wenn eine Aufgabe in einem ThreadPool-Thread ausgeführt wird, wird sie beendet, und der Thread wird an den Threadpool zurückgegeben. Die unbehandelte Ausnahme darf die Anwendung nicht beenden.

  • Wenn ein Thread in nicht verwaltetem Code gestartet wurde, z. B. als Hauptanwendungsthread, wird er beendet. Die unbehandelte Ausnahme kann fortfahren, und die Anwendung wird vom Betriebssystem beendet.

AppDomainUnloadedException verwendet HRESULT COR_E_APPDOMAINUNLOADED mit dem Wert 0x80131014.

Eine Liste der anfänglichen Eigenschaftenwerte für eine Instanz von AppDomainUnloadedException finden Sie unter den AppDomainUnloadedException-Konstruktoren.

Beispiel

Dieser Abschnitt enthält zwei Codebeispiele. Im ersten Beispiel werden die Auswirkungen einer AppDomainUnloadedException auf verschiedene Threads deutlich, und im zweiten Beispiel wird das Entladen der elementaren Anwendungsdomäne gezeigt.

Beispiel 1

Im folgenden Codebeispiel wird eine TestClass-Klasse definiert, die über die Grenzen der Anwendungsdomäne hinweg gemarshallt werden kann, und eine Example-Klasse, die eine static (Shared in Visual Basic) ThreadProc-Methode enthält. Die ThreadProc-Methode erstellt eine Anwendungsdomäne, erstellt ein TestClass-Objekt in der Domäne und ruft eine Methode von TestClass auf, die die ausführende Domäne entlädt. Dadurch wird eine AppDomainUnloadedException ausgelöst.

Die TestClass-Methode wird ohne Ausnahmebehandlung aus einem ThreadPool-Thread und aus einem normalen Thread ausgeführt. Damit wird gezeigt, dass die unbehandelte Ausnahme die Aufgabe oder den Thread beendet, aber nicht die Anwendung. Sie wird dann mit und ohne Ausnahmebehandlung aus dem Hauptanwendungsthread ausgeführt. Damit wird gezeigt, dass die Anwendung beendet wird, wenn sie nicht behandelt wird.

Imports System
Imports System.Threading
Imports System.Runtime.InteropServices

Public Class Example
    
    Public Shared Sub Main() 

        ' 1. Queue ThreadProc as a task for a ThreadPool thread.
        ThreadPool.QueueUserWorkItem(AddressOf ThreadProc, _
            " from a ThreadPool thread")
        Thread.Sleep(1000)
        
        ' 2. Execute ThreadProc on an ordinary thread.
        Dim t As New Thread(AddressOf ThreadProc)
        t.Start(" from an ordinary thread")
        t.Join()
        
        ' 3. Execute ThreadProc on the main thread, with 
        '    exception handling.
        Try
            ThreadProc(" from the main application thread (handled)")
        Catch adue As AppDomainUnloadedException
            Console.WriteLine("Main thread caught AppDomainUnloadedException: {0}", _
                adue.Message)
        End Try
        
        ' 4. Execute ThreadProc on the main thread without
        '    exception handling.
        ThreadProc(" from the main application thread (unhandled)")
        
        Console.WriteLine("Main: This message is never displayed.")
    
    End Sub 
    
    Private Shared Sub ThreadProc(ByVal state As Object) 
        ' Create an application domain, and create an instance
        ' of TestClass in the application domain. The first
        ' parameter of CreateInstanceAndUnwrap is the name of
        ' this executable. If you compile the example code using
        ' any name other than "Sample.exe", you must change the
        ' parameter appropriately.
        Dim ad As AppDomain = AppDomain.CreateDomain("TestDomain")
        Dim o As Object = ad.CreateInstanceAndUnwrap("Sample", "TestClass")
        Dim tc As TestClass = CType(o, TestClass)
        
        ' In the new application domain, execute a method that
        ' unloads the AppDomain. The unhandled exception this
        ' causes ends the current thread.
        tc.UnloadCurrentDomain(state)
        
        Console.WriteLine("ThreadProc: This message is never displayed.")
    
    End Sub
End Class 

' TestClass derives from MarshalByRefObject, so it can be marshaled
' across application domain boundaries. 
'
Public Class TestClass
    Inherits MarshalByRefObject
    
    Public Sub UnloadCurrentDomain(ByVal state As Object) 
        Console.WriteLine(vbLf & "Unloading the current AppDomain{0}.", state)
        
        ' Unload the current application domain. This causes
        ' an AppDomainUnloadedException to be thrown.
        '
        AppDomain.Unload(AppDomain.CurrentDomain)
    
    End Sub 
End Class 

' This code example produces output similar to the following:
'
'Unloading the current AppDomain from a ThreadPool thread.
'
'Unloading the current AppDomain from an ordinary thread.
'
'Unloading the current AppDomain from the main application thread (handled).
'Main thread caught AppDomainUnloadedException: The application domain in which the thread was running has been unloaded.
'
'Unloading the current AppDomain from the main application thread (unhandled).
'
'Unhandled Exception: System.AppDomainUnloadedException: The application domain in which the thread was running has been unloaded.
'   at TestClass.UnloadCurrentDomain(Object state)
'   at Example.ThreadProc(Object state)
'   at Example.Main()
' 
using System;
using System.Threading;
using System.Runtime.InteropServices;

public class Example
{
    public static void Main()
    {
        // 1. Queue ThreadProc as a task for a ThreadPool thread.
        ThreadPool.QueueUserWorkItem(ThreadProc, " from a ThreadPool thread");
        Thread.Sleep(1000);

        // 2. Execute ThreadProc on an ordinary thread.
        Thread t = new Thread(ThreadProc);
        t.Start(" from an ordinary thread");
        t.Join();

        // 3. Execute ThreadProc on the main thread, with 
        //    exception handling.
        try
        {
            ThreadProc(" from the main application thread (handled)");
        }
        catch (AppDomainUnloadedException adue)
        {
            Console.WriteLine("Main thread caught AppDomainUnloadedException: {0}", adue.Message);
        }

        // 4. Execute ThreadProc on the main thread without
        //    exception handling.
        ThreadProc(" from the main application thread (unhandled)");

        Console.WriteLine("Main: This message is never displayed.");
    }

    private static void ThreadProc(object state)
    {
        // Create an application domain, and create an instance
        // of TestClass in the application domain. The first
        // parameter of CreateInstanceAndUnwrap is the name of
        // this executable. If you compile the example code using
        // any name other than "Sample.exe", you must change the
        // parameter appropriately.
        AppDomain ad = AppDomain.CreateDomain("TestDomain");
        TestClass tc = (TestClass)ad.CreateInstanceAndUnwrap("Sample", "TestClass");

        // In the new application domain, execute a method that
        // unloads the AppDomain. The unhandled exception this
        // causes ends the current thread.
        tc.UnloadCurrentDomain(state);

        Console.WriteLine("ThreadProc: This message is never displayed.");
    }
}

// TestClass derives from MarshalByRefObject, so it can be marshaled
// across application domain boundaries. 
//
public class TestClass : MarshalByRefObject
{
    public void UnloadCurrentDomain(object state)
    {
        Console.WriteLine("\nUnloading the current AppDomain{0}.", state);
 
        // Unload the current application domain. This causes
        // an AppDomainUnloadedException to be thrown.
        //
        AppDomain.Unload(AppDomain.CurrentDomain);
    }
}

/* This code example produces output similar to the following:
Unloading the current AppDomain from a ThreadPool thread.

Unloading the current AppDomain from an ordinary thread.

Unloading the current AppDomain from the main application thread (handled).
Main thread caught AppDomainUnloadedException: The application domain in which the thread was running has been unloaded.

Unloading the current AppDomain from the main application thread (unhandled).

Unhandled Exception: System.AppDomainUnloadedException: The application domain in which the thread was running has been unloaded.
   at TestClass.UnloadCurrentDomain(Object state)
   at Example.ThreadProc(Object state)
   at Example.Main()
 */

Beispiel 2

Im folgenden Codebeispiel wird eine Anwendungsdomäne erstellt und entladen. Es wird gezeigt, dass eine AppDomainUnloadedException bei einem nachfolgenden Versuch des Zugriffs auf die entladene Domäne ausgelöst wird.

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

Vererbungshierarchie

System.Object
   System.Exception
     System.SystemException
      System.AppDomainUnloadedException

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

AppDomainUnloadedException-Member
System-Namespace
AppDomain-Klasse
Exception

Weitere Ressourcen

Behandeln und Auslösen von Ausnahmen