CHAPTER 17 PROCESSES, APPDOMAINS, AND OBJECT CONTEXTS
ProcessExit
This occurs on the default application domain when the default
application domain’s parent process exits.
UnhandledException
This occurs when an exception is not caught by an exception handler.
Interacting with the Default Application Domain
Recall that when a .NET executable starts, the CLR will automatically place it into the default
AppDomain of the hosting process. This is done automatically and transparently, and you never have to
author any specific code to do so. However, it is possible for your application to gain access to this
default application domain using the static AppDomain.CurrentDomain property. After you have this
access point, you are able to hook into any events of interest, or make use of the methods and properties
of AppDomain to perform some runtime diagnostics.
To learn how to interact with the default application domain, begin by creating a new Console
Application named DefaultAppDomainApp. Now, update your program with the following logic, which
will simply display some details about the default application domain, using a number of members of
the AppDomain class:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with the default AppDomain *****\n");
DisplayDADStats();
Console.ReadLine();
}
private static void DisplayDADStats()
{
// Get access to the AppDomain for the current thread.
AppDomain defaultAD = AppDomain.CurrentDomain;
// Print out various stats about this domain.
Console.WriteLine("Name of this domain: {0}", defaultAD.FriendlyName);
Console.WriteLine("ID of domain in this process: {0}", defaultAD.Id);
Console.WriteLine("Is this the default domain?: {0}",
defaultAD.IsDefaultAppDomain());
Console.WriteLine("Base directory of this domain: {0}", defaultAD.BaseDirectory);
}
}
The output of this example can be seen here:
638