Parallel Computing in .NET 4

Almost all computers today have multiple CPU cores. .NET 4 allows you to easily take advantage of all that computing power, very easily.  One way is to use the Parallel class, which is included in the System.Threading.Tasks namespace.

Let’s take a look at an example.  In the method below, all that happens is a number gets squared and is sent to the screen.  To simulate a long running task, each time the method is called the thread goes to sleep for random amounts of time between 100 and 1000 milliseconds.

private void Square(int i)


    Random rnd = new Random();

    int wait = rnd.Next(1, 10) * 100;


    Console.WriteLine(i * i);


Inside the same class, is a method called Execute() that uses a for-each loop to square for the numbers 1 through 10.  It is shown below.

public void Execute()


    var integers = Enumerable.Range(1, 10);

    foreach (var i in integers)





In the following code Execute() is called, and each squared number is put on the screen.  Then the time it takes to run through the loop is calculated and displayed as well.

DateTime start = DateTime.Now;

LongCommand cmd = new LongCommand();


TimeSpan elapsed =  DateTime.Now.Subtract(start);


    String.Format("It took {0} milliseconds.",


The output is shown below.  It varies each time, but takes around four seconds for the code to complete.  Notice, as you would expect, the squared numbers come out in sequence.

Loop using 1 CPU core

Below is a variation of the prior Execute() method, ExecuteInParallel().  However, instead of using a for-each loop, it uses the Parallel class ForEach() method.  This method will take advantage of the CPU cores it has available to it.  The collection of numbers is passed as an argument to ForEach(), along with a lambda expression that calls Square() on each number in the collection. (If you don’t understand lambda expressions, see my previous post:Understanding Lambda Expressions in C#).

public void ExecuteInParallel()


    var integers = Enumerable.Range(1, 10);

    Parallel.ForEach(integers, i => Square(i))


Now, if you call ExecuteInParallel(), rather than Execute() the output is shown below.  This was run on a machine with a dual-core CPU.  As you would expect it takes about half as long to run.  Also notice, the numbers don’t come out in order.  This proves that both cores are being utilized.

Loop using 2 CPU cores

You can download the code from my web site at:

If you’re not a .NET Programmer, but you would like to learn more about it, attend Learning Tree course 502: Programming with .NET: A Comprehensive Hands-On Introduction.

If you are a .NET programmer, and you would like to learn more about writing .NET applications for Windows Azure, come to Learning Tree course 2602: Windows Azure Platform Introduction: Programming Cloud-Based Applications.

Doug Rehnstrom

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.