AppDomainUnloadedException Klas

Definitie

De uitzondering die wordt gegenereerd wanneer een poging wordt gedaan om toegang te krijgen tot een niet-geladen toepassingsdomein.

public ref class AppDomainUnloadedException : SystemException
public class AppDomainUnloadedException : SystemException
[System.Serializable]
public class AppDomainUnloadedException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class AppDomainUnloadedException : SystemException
type AppDomainUnloadedException = class
    inherit SystemException
[<System.Serializable>]
type AppDomainUnloadedException = class
    inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type AppDomainUnloadedException = class
    inherit SystemException
Public Class AppDomainUnloadedException
Inherits SystemException
Overname
AppDomainUnloadedException
Kenmerken

Voorbeelden

Deze sectie bevat twee codevoorbeelden. In het eerste voorbeeld ziet u de effecten van een AppDomainUnloadedException op verschillende threads en het tweede voorbeeld toont het lossen van elementaire toepassingsdomeinen.

Voorbeeld 1

In het volgende codevoorbeeld wordt een TestClass-klasse gedefinieerd die kan worden gemarsht over de grenzen van het toepassingsdomein en een klasse Example met een static (Shared in Visual Basic) ThreadProc methode. De ThreadProc methode maakt een toepassingsdomein, maakt een TestClass object in het domein en roept een methode aan waarmee TestClass het uitgevoerde domein wordt verwijderd, waardoor een AppDomainUnloadedException.

De TestClass methode wordt uitgevoerd zonder uitzonderingsafhandeling van een ThreadPool thread en vanuit een gewone thread, waarmee wordt gedemonstreerd dat de niet-verwerkte uitzondering de taak of thread beëindigt, maar niet de toepassing. Het wordt vervolgens uitgevoerd met en zonder uitzonderingsafhandeling van de hoofdtoepassingsthread, waarmee wordt gedemonstreerd dat de toepassing wordt beëindigd als deze niet wordt verwerkt.

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()
 */
open System
open System.Threading

// TestClass derives from MarshalByRefObject, so it can be marshaled
// across application domain boundaries.
type TestClass() =
    inherit MarshalByRefObject()
    member _.UnloadCurrentDomain (state: obj) =
        printfn $"\nUnloading the current AppDomain{state}."

        // Unload the current application domain. This causes
        // an AppDomainUnloadedException to be thrown.
        //
        AppDomain.Unload AppDomain.CurrentDomain

let threadProc (state: obj) =
    // 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.
    let ad = AppDomain.CreateDomain "TestDomain"
    let tc = ad.CreateInstanceAndUnwrap("Sample", "TestClass") :?> 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

    printfn "ThreadProc: This message is never displayed."

// 1. Queue ThreadProc as a task for a ThreadPool thread.
ThreadPool.QueueUserWorkItem(threadProc, " from a ThreadPool thread") |> ignore
Thread.Sleep 1000

// 2. Execute ThreadProc on an ordinary thread.
let t = Thread(ParameterizedThreadStart 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)"
with :? AppDomainUnloadedException as adue ->
    printfn $"Main thread caught AppDomainUnloadedException: {adue.Message}"

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

printfn "Main: This message is never displayed."

(* 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()
 *)
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()
'

Voorbeeld 2

In het volgende codevoorbeeld wordt een toepassingsdomein gemaakt en uitgepakt en gedemonstreerd dat er een AppDomainUnloadedException wordt gegenereerd bij een volgende poging om toegang te krijgen tot het niet-geladen domein.

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

Opmerkingen

In het .NET Framework versie 2.0 heeft een AppDomainUnloadedException die niet wordt verwerkt in de gebruikerscode het volgende effect:

  • Als een thread is gestart in beheerde code, wordt deze beëindigd. De niet-verwerkte uitzondering mag de toepassing niet beëindigen.

  • Als een taak wordt uitgevoerd op een ThreadPool thread, wordt deze beëindigd en wordt de thread geretourneerd naar de threadgroep. De niet-verwerkte uitzondering mag de toepassing niet beëindigen.

  • Als een thread is gestart in niet-beheerde code, zoals de hoofdtoepassingsthread, wordt deze beëindigd. De niet-verwerkte uitzondering mag doorgaan en het besturingssysteem beëindigt de toepassing.

AppDomainUnloadedException maakt gebruik van de HRESULT-COR_E_APPDOMAINUNLOADED, die de waarde 0x80131014 heeft.

Voor een lijst van initiële eigenschapswaarden voor een exemplaar van AppDomainUnloadedException, zie de AppDomainUnloadedException-constructoren.

Constructors

Name Description
AppDomainUnloadedException()

Initialiseert een nieuw exemplaar van de AppDomainUnloadedException klasse.

AppDomainUnloadedException(SerializationInfo, StreamingContext)
Verouderd.

Initialiseert een nieuw exemplaar van de AppDomainUnloadedException klasse met geserialiseerde gegevens.

AppDomainUnloadedException(String, Exception)

Initialiseert een nieuw exemplaar van de AppDomainUnloadedException klasse met een opgegeven foutbericht en een verwijzing naar de binnenste uitzondering die de oorzaak van deze uitzondering is.

AppDomainUnloadedException(String)

Initialiseert een nieuw exemplaar van de AppDomainUnloadedException klasse met een opgegeven foutbericht.

Eigenschappen

Name Description
Data

Hiermee haalt u een verzameling sleutel-waardeparen op die aanvullende door de gebruiker gedefinieerde informatie over de uitzondering bieden.

(Overgenomen van Exception)
HelpLink

Hiermee haalt u een koppeling op naar het Help-bestand dat aan deze uitzondering is gekoppeld.

(Overgenomen van Exception)
HResult

Hiermee wordt HRESULT opgehaald of ingesteld, een gecodeerde numerieke waarde die is toegewezen aan een specifieke uitzondering.

(Overgenomen van Exception)
InnerException

Hiermee haalt u het Exception exemplaar op dat de huidige uitzondering heeft veroorzaakt.

(Overgenomen van Exception)
Message

Hiermee wordt een bericht weergegeven waarin de huidige uitzondering wordt beschreven.

(Overgenomen van Exception)
Source

Hiermee wordt de naam van de toepassing of het object dat de fout veroorzaakt, opgehaald of ingesteld.

(Overgenomen van Exception)
StackTrace

Hiermee haalt u een tekenreeksweergave van de directe frames op de aanroepstack op.

(Overgenomen van Exception)
TargetSite

Hiermee haalt u de methode op waarmee de huidige uitzondering wordt gegenereerd.

(Overgenomen van Exception)

Methoden

Name Description
Equals(Object)

Bepaalt of het opgegeven object gelijk is aan het huidige object.

(Overgenomen van Object)
GetBaseException()

Wanneer deze wordt overschreven in een afgeleide klasse, retourneert u de Exception hoofdoorzaak van een of meer volgende uitzonderingen.

(Overgenomen van Exception)
GetHashCode()

Fungeert als de standaardhashfunctie.

(Overgenomen van Object)
GetObjectData(SerializationInfo, StreamingContext)
Verouderd.

Wanneer deze wordt overschreven in een afgeleide klasse, stelt u de SerializationInfo met informatie over de uitzondering in.

(Overgenomen van Exception)
GetType()

Hiermee haalt u het runtimetype van het huidige exemplaar op.

(Overgenomen van Exception)
MemberwiseClone()

Hiermee maakt u een ondiepe kopie van de huidige Object.

(Overgenomen van Object)
ToString()

Hiermee maakt en retourneert u een tekenreeksweergave van de huidige uitzondering.

(Overgenomen van Exception)

gebeurtenis

Name Description
SerializeObjectState
Verouderd.

Treedt op wanneer een uitzondering wordt geserialiseerd om een uitzonderingsstatusobject te maken dat geserialiseerde gegevens over de uitzondering bevat.

(Overgenomen van Exception)

Van toepassing op

Zie ook