03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

486 ❘ CHAPTER 22 Parallel Programming<br />

Multithreading Advantages<br />

Multithreading has other advantages in addition to executing code in parallel. For<br />

example, many programs perform tasks that take a long time to finish. Perhaps the<br />

program needs to check a dozen websites to get pricing information for a particular<br />

product. If the program does everything in a single thread, then the main program<br />

is blocked until the web search finishes. That means the user interface is stuck and<br />

the user can’t do anything except watch the screen refuse to refresh. If you run the<br />

user interface and the web search on separate threads, the program can remain<br />

responsive even while the search is still running.<br />

Multithreading can also sometimes simplify your code. Suppose a program needs<br />

to monitor several processes. Perhaps it needs to periodically check a collection<br />

of websites for news and stock prices. You could write a program that repeatedly<br />

loops through each of the websites to check them one after another. That might be<br />

somewhat complicated because the code to deal with each website would be intermingled<br />

with the code needed to loop through the sites, handle timeouts, and deal<br />

with other potential problems on each site.<br />

Another approach would be to assign a separate thread to each website and let each<br />

thread run independently. Now each thread’s code can focus on a single website.<br />

Also if one thread’s website is having problems, it won’t affect the performance of<br />

the other threads.<br />

The .NET Framework provides several methods for multithreading including the following.<br />

➤➤<br />

➤➤<br />

➤➤<br />

➤➤<br />

➤➤<br />

PLINQ—You saw this in Chapter 8, “LINQ.”<br />

BackgroundWorker—This component executes code on a separate thread. It uses events to<br />

communicate with the main user interface thread (the UI thread).<br />

Task Parallel Library (TPL)—These tools let you easily run multiple methods in different<br />

threads or run multiple instances of the same method with different parameters.<br />

Tasks—The Task class lets you create and run threads. The Task class defines some methods<br />

to make certain typical operations easier such as creating and starting a task in a single step,<br />

waiting for a group of tasks to finish, or waiting until any one of a group of tasks finishes.<br />

Threads—The Thread class gives you lower level access to threads. These are more<br />

complicated that using other methods but they provide greater control.<br />

This chapter explains how you can use BackgroundWorker, TPL, threads, and tasks to execute code on<br />

multiple threads simultaneously. (See the section “PLINQ” in Chapter 8 for information about PLINQ.)<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!