FREE BOOK

Chapter 10: Processes, AppDomains,Contexts, and Threads

Posted by Apress Free Book | C#.NET February 02, 2009
In this Chapter you drill deeper into the constitution of a .NET executable host and come to understand the relationship between Win32 processes, application domains, contexts, and threads.

Multithreaded Programming via Delegates

Before checking out the details of programming with threads under the .NET platform, it is worth reiterating that the whole point of creating additional threads is to increase the overall functionality of a given application to the user. Recall, however, that many common programming tasks that traditionally required manual creation of threads (e.g., remote method invocations, manipulating IO streams, and so forth) are automated using asynchronous delegates (first examined in Chapter 7).

As you have seen throughout various points in this text (and will see in future chapters), when you invoke a delegate asynchronously, the CLR automatically creates a worker thread to handle the task at hand. However, if you have an application-specific task to account for (such as printing a lengthy document or working with a GUI display), you will be required to manually manipulate threads if you wish to keep your primary thread responsive. This disclaimer aside, allow me to formally introduce
the System.Threading namespace.

The System.Threading Namespace

Under the .NET platform, the System.Threading namespace provides a number of types that enable multithreaded programming. In addition to providing types that represent a specific CLR thread, this namespace also defines types that can manage a collection of threads (ThreadPool), a simple (non-GUI based) Timer class, and various types used to provide synchronized access to shared resources. Table 10-6 lists some (but not all) of the core members of this namespace.

Table 10-6. Select Types of the System.Threading Namespace

System.Threading Type Meaning in Life
Interlocked Provides atomic operations for objects that are shared by multiple threads.
Monitor Provides the synchronization of threading objects using locks and wait/signals.
Mutex Synchronization primitive that can be used for interprocess synchronization.
Thread Represents a thread that executes within the CLR. Using this type, you are able to spawn additional threads in the originating AppDomain.
ThreadPool This type manages related threads in a given process.
Timer Provides a mechanism for executing a method at specified intervals.
ThreadStart Delegate that specifies the method to call for a given Thread.
ThreadState This enum specifies the valid states a thread may take (Running, Aborted, etc.).
TimerCallback Delegate type used in conjunction with Timer types.
ThreadPriority This enum specifies the valid levels of thread priority.

Examining the Thread Class

The most primitive of all types in the System.Threading namespace is Thread. This class represents an object-oriented wrapper around a given path of execution within a particular AppDomain. This type also defines a number of methods (both static and shared) that allow you to create new threads from the scope of the current thread, as well as suspend, stop, and destroy a particular thread. Consider the list of core static members given in Table 10-7.

Table 10-7. Key Static Members of the Thread Type

Thread Static Member Meaning in Life
CurrentContext This (read-only) property returns the context the thread is currently running.
CurrentThread This (read-only) property returns a reference to the currently running thread.
GetDomain()
GetDomainID()
Returns a reference to the current AppDomain (or the ID of this domain) in which the current thread is running.
Sleep() Suspends the current thread for a specified time.

Thread also supports the object level members shown in Table 10-8.

Table 10-8. Select Instance Level Members of the Thread Type

Thread Instance Level Member Meaning in Life
Abort() This method instructs the CLR to terminate the thread ASAP.
IsAlive This property returns a Boolean that indicates if this thread has been started.
IsBackground Gets or sets a value indicating whether or not this thread is a background thread.
Name This property allows you to establish a friendly textual name of the thread.
Priority Gets or Sets the priority of a thread, which may be assigned a value from the ThreadPriority enumeration.
ThreadState Gets the state of this thread, which may be assigned a value from the ThreadState enumeration.
Interrupt() Interrupts the current thread.
Join() Instructs a thread to wait for another thread to complete.
Resume() Resumes a thread that has been previously suspended.
Start() Instructs the CLR to execute the thread ASAP.
Suspend() Suspends the thread. If the thread is already suspended, a call to Suspend() has no effect.

Total Pages : 13 7891011

comments