Multithreaded Programming via DelegatesBefore 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 introducethe System.Threading namespace.The System.Threading NamespaceUnder 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
Examining the Thread ClassThe 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 also supports the object level members shown in Table 10-8. Table 10-8. Select Instance Level Members of the Thread Type