CHAPTER 19 MULTITHREADED, PARALLEL, AND ASYNC PROGRAMMING
}
Thread currThread = Thread.CurrentThread;
Under the .NET platform, there is not a direct one-to-one correspondence between application
domains and threads. In fact, a given AppDomain can have numerous threads executing within it at any
given time. Furthermore, a particular thread is not confined to a single application domain during its
lifetime. Threads are free to cross application domain boundaries as the Windows OS thread scheduler
and the .NET CLR see fit.
Although active threads can be moved between AppDomain boundaries, a given thread can execute
within only a single application domain at any point in time (in other words, it is impossible for a single
thread to be doing work in more than one AppDomain at once). When you want to programmatically
gain access to the AppDomain that is hosting the current thread, call the static Thread.GetDomain()
method, like so:
static void ExtractAppDomainHostingThread()
{
// Obtain the AppDomain hosting the current thread.
AppDomain ad = Thread.GetDomain();
}
A single thread may also be moved into a particular context at any given time, and it may be
relocated within a new context at the whim of the CLR. When you want to obtain the current context a
thread happens to be executing in, make use of the static Thread.CurrentContext property (which
returns a System.Runtime.Remoting.Contexts.Context object), l