There are essentially two ways to take advantage of multithreading with .NET:
This section focuses on the second approach, which provides most of the benefits of custom threading and shields you from many of the risks. Later in the chapter, we'll examine the first approach and custom threading. Of course, it's also worth noting that many parts of a .NET application gain the benefits of threading completely for free. When you use the .NET printing classes, for example, the PrintDocument.PrintPage event is fired on a separate thread, allowing your application to remain responsive. Similarly, when you are hosting Web pages or XML Web services through ASP.NET, or components through .NET Remoting, the common language runtime (CLR) automatically uses a pool of worker threads to handle the requests of multiple clients simultaneously. These threads run separate instances of the component, which means that your code rarely needs to take this concurrent use into account. Similarly, if you are offloading the work from your applications to a relational database such as Microsoft SQL Server, you'll benefit from a tested, optimized product that uses a pool of worker threads to efficiently handle high request volumes and intensive file access. |