Book Image

C# Multithreaded and Parallel Programming

By : Rodney Ringler
Book Image

C# Multithreaded and Parallel Programming

By: Rodney Ringler

Overview of this book

<p>Most modern machines have dual-core processors. This means that the present-day computer has the ability to multitask. Using multiple cores means your applications can process data faster and be more responsive to users. However, to fully exploit this in your applications, you need to write multithreading code.</p> <p>We will begin by covering some techniques that have been around since the beginning of .NET, including the BackgroundWorker component, timers, and the Thread class. We will use tasks, task factories, and parallel loops to develop multithreaded applications at a higher level than directly creating and managing individual threads. Finally, we will look at the tools Visual Studio provides for debugging parallel applications, common concurrent design patterns, and the latest updates in PLINQ and async.</p>
Table of Contents (18 chapters)
C# Multithreaded and Parallel Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Joining threads


We have already examined joining a thread in the previous chapter. When you join a thread, you tell the current thread to wait on the thread, which you are joining, to complete. This allows you to coordinate work between two threads.

This was very handy in the example where we wanted to know when one piece is complete before we start the next piece.

For our application, let's say we want to examine how the performance changes if all of the bitmap processing threads run sequentially instead of concurrently.

How to do it

If we want all of our bitmap processing threads to run sequentially, then right after we start a thread, we will join the thread. This will halt execution of the main thread until the bitmap processing thread we just started has finished. To do this, change the bottom for loop in the butFindOldStars_Click event handler and add the following line:

prloThreadList[liThreadNumber].Join();

So, now the for loop looks like this:

            // Now, start the threads
   ...