System.AppDomain Class

public sealed class AppDomain : MarshalByRefObject

Base Types

Object
  MarshalByRefObject
    AppDomain

Assembly

mscorlib

Library

RuntimeInfrastructure

Summary

Represents an application domain, which is an isolated environment where applications execute.

Description

Application domains, which are represented by AppDomain objects, provide isolation, unloading, and security boundaries for executing managed code.

Multiple application domains can run in a single process; however, there is not a one-to-one correlation between application domains and threads. Several threads can belong to a single application domain, and while a given thread is not confined to a single application domain, at any given time, a thread executes in a single application domain.

Application domains are created using the CreateDomain method. AppDomain instances are used to load and execute assemblies (Assembly). When a AppDomain is no longer in use, it can be unloaded.

The AppDomain class implements a set of events to enable applications to respond to the following conditions:

ConditionEvent
An assembly was loaded.

System.AppDomain.AssemblyLoad
An application domain will be unloaded. System.AppDomain.DomainUnload
An unhandled exception was thrown. System.AppDomain.UnhandledException

See Also

System Namespace

Members

AppDomain Methods

AppDomain.CreateDomain Method
AppDomain.ToString Method
AppDomain.Unload Method

AppDomain Properties

AppDomain.FriendlyName Property

AppDomain Events

AppDomain.AssemblyLoad Event
AppDomain.DomainUnload Event
AppDomain.UnhandledException Event


AppDomain.CreateDomain Method

public static AppDomain CreateDomain(string friendlyName);

Summary

Creates and returns a new application domain with the specified name.

Parameters

friendlyName
A String containing the friendly name of the domain.

Return Value

A AppDomain representing the newly created application domain.

Exceptions

Exception TypeCondition
ArgumentNullExceptionfriendlyName is null .

Description

[Note: The friendlyName parameter is intended to identify the domain in a manner that is meaningful to humans. This string should be suitable for display in user interfaces. ]

See Also

System.AppDomain Class, System Namespace

AppDomain.ToString Method

public override string ToString();

Summary

Returns a String representation of the current instance.

Return Value

A String containing information about the current AppDomain instance.

Description

The string returned by this method includes the friendly name of the application domain.

[Note: This method overrides System.Object.ToString .]

See Also

System.AppDomain Class, System Namespace

AppDomain.Unload Method

public static void Unload(AppDomain domain);

Summary

Unloads the specified application domain.

Parameters

domain
A AppDomain representing the application domain to be unloaded.

Exceptions

Exception TypeCondition
ArgumentNullExceptiondomain is null .
CannotUnloadAppDomainExceptiondomain could not be unloaded.

Description

If the thread that invoked System.AppDomain.Unload(System.AppDomain) is running in domain, another thread is started to perform the unload operation. If domain cannot be unloaded, a CannotUnloadAppDomainException is thrown in that thread, not the original thread that invoked System.AppDomain.Unload(System.AppDomain). However, if the thread that invoked System.AppDomain.Unload(System.AppDomain) is running outside domain, that is the thread that receives the exception.

The threads in domain are terminated using the System.Threading.Thread.Abort(System.Object) method, which throws the thread an instance of ThreadAbortException. [Note: Although the thread should terminate promptly, it can continue executing for an unbounded amount of time in its finally clause.]

See Also

System.AppDomain Class, System Namespace

AppDomain.FriendlyName Property

public string FriendlyName { get; }

Summary

Gets the friendly name of the current instance.

Property Value

A String containing the friendly name of the current application domain.

Description

This property is read-only.

The friendly name of a AppDomain instance created by an application is specified to the constructor. The friendly name of the default AppDomain is the name of the assembly file loaded in the application domain. The friendly name is formed by stripping the directory information from the assembly's file name. For example, if the loaded assembly has the name "\MyAppDirectory\MyAssembly.exe" , the friendly name of the default application domain is "MyAssembly.exe" .

See Also

System.AppDomain Class, System Namespace

AppDomain.AssemblyLoad Event

public event AssemblyLoadEventHandler AssemblyLoad

Summary

Raised when an assembly is loaded.

Description

[Note: This event is handled by a AssemblyLoadEventHandler delegate. Information about the event is passed to the delegate in a AssemblyLoadEventArgs instance.

For additional information about events, see Partitions I and II of the CLI Specification.

]

See Also

System.AppDomain Class, System Namespace

AppDomain.DomainUnload Event

public event EventHandler DomainUnload

Summary

Raised when a AppDomain is about to be unloaded.

Description

[Note: This event is handled by a EventHandler delegate. Information about the event is passed to the delegate in a EventArgs instance. The delegate for this event can perform any termination activities before the application domain is unloaded.

For additional information about events, see Partitions I and II of the CLI Specification.

]

See Also

System.AppDomain Class, System Namespace

AppDomain.UnhandledException Event

public event UnhandledExceptionEventHandler UnhandledException

Summary

Raised when an exception is not caught by the default application domain.

Description

[Note: This event is handled by a UnhandledExceptionEventHandler delegate. Information about the event is passed to the delegate in a UnhandledExceptionEventArgs instance. The delegate provides default handling for uncaught exceptions. When this event is not handled, the system default handler reports the exception to the user and might terminate the application. For additional information, see System.UnhandledExceptionEventArgs.IsTerminating. ]

This event is raised only for the application domain that is created by the system when an application is started. If an application creates additional application domains, specifying a delegate for this event in those applications domains has no effect.

[Note: For additional information about events, see Partitions I and II of the CLI Specification.]

See Also

System.AppDomain Class, System Namespace