Queuing work items for the thread pool static void

Info icon This preview shows pages 645–647. Sign up to view the full content.

Example 16-6. Queuing work items for the thread pool static void Main(string[] args) { ThreadPool.QueueUserWorkItem(Go, "One"); ThreadPool.QueueUserWorkItem(Go, "Two"); Go("Main"); // Problem: not waiting for work items to complete! } This example has a problem: if the main thread finishes first, the program may exit before the thread pool work items complete. So this only illustrates how to start work on the thread pool. This might not be a problem in practice—it depends on your ap- plication’s typical life cycle, but you may need to add additional code to coordinate completion. Running on a quad-core machine, this particular example behaves in much the same way as the previous ones, because the thread pool ends up creating a thread for both work items. On a single-core machine, you might see a difference—it could decide to let the first item run to completion and then run the second afterward. The thread pool is designed for fairly short pieces of work. One of the most important jobs it was originally introduced for was to handle web requests in ASP.NET web ap- plications, so if you’re wondering how much work constitutes “fairly short,” a reason- able answer is “about as much work as it takes to generate a web page.” .NET 4 introduces a new way to use the thread pool, the Task Parallel Library, which offers a couple of advantages. First, it handles certain common scenarios more effi- ciently than QueueUserWorkItem . Second, it offers more functionality. For example, tasks have much more comprehensive support for handling errors and completion, issues Example 16-6 utterly fails to address. If the main thread finishes before either of the work items is complete, that example will simply exit without waiting for them! And if you want the main thread to discover exceptions that occurred on the thread pool threads, there’s no easy way to do that. If any of these things is important to you, the Task Parallel Library is a better way to use the thread pool. There’s a whole section on that later in this chapter. For now, we’ll continue looking at some aspects of threading that you need to know, no matter what multithreading mechanisms you may be using. Threads | 621
Image of page 645

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Thread Affinity and Context Not all threads are equal. Some work can be done on only certain threads. For example, WPF and Windows Forms both impose a similar requirement: an object representing something in the user interface must be used only on the thread that created that object in the first place. These objects have thread affinity , meaning that they belong to one particular thread. Not all things with thread affinity are quite as obstinate as user interface elements. For example, while some COM objects have thread affinity issues, they are usually more flexible. (COM, the Component Object Model, is the basis of various Windows technologies including ActiveX controls. It predates .NET, and we’ll see how to use it from .NET in Chapter 19 .) .NET handles some of the COM thread affinity work for you, making it possible to use a COM object from any thread. The main
Image of page 646
Image of page 647
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern