C# 4.0 - Beginners look into parallel programming

DZone 's Guide to

C# 4.0 - Beginners look into parallel programming

· ·
Free Resource
When C# 4.0 was released we C# developers were given a new toy. to play with, and that's Parallel Programming. This is done with the System.Threading.Tasks.Parallel Namespace. This allows for parallel loops & regions (to be discussed at a later date). What is parallel programming you ask, well in todays age most, if not all, household computers have multi-core processors, and parallel programming allows us to take davantage of this new found power, well not actually new as multi-core processors have been around a while now, but software has not kept up with the changes in hardware.

Used to we had to use multi-threading for something that resembled synchronously operations. This was the norm for programming, but things are changing and evolving and Microsoft has finally given us the ability to take advantage of multi-core processors on the systems we're programming for. Now to take advantage of this you will need the .Net 4.0 Framework and Visual Studio 2010. Before we go on I will let you know that there are extensions ( Reactive Extensions for .Net) that allow you to access this from C# 3.5 but for the purpose of this article we're referring to 4.0 only.

NOTE: In all honesty you should only use parallel when you have a long, intensive process. If you use it for small, easy loops & tasks you could actuall slow down the overall process.

First think we want to do is create ourself a new Visual Studio 2010 project (I'm using a console application for this sample) so get that open and create your new project. Now we want to add a reference to the System.Threading.Tasks Namespace, to do this go Project > Add Reference and select System.Threading.  Then in your code file add
using System.Threading.Tasks;
This gives us access to what we need for parallel work. Noe, for this introduction we're going to count the number of files in a given directory, including the subdirectories in said directory. We're going to store all the directories in a Stack<string> just for simplicity sake. For those who have never used the Stack this is a LIFO (last-in-first-out) collection, in this case of type string.

For traversing the directories in a parallel fashion we'll use Parallel.Foreach Method, which executes a for each operation in a parallel iteration. We will use the overloaded version
 Parallel.ForEach(TSource, TLocal) Method (Partitioner(TSource), ParallelOptions, Func(TLocal), Func(TSource, ParallelLoopState, TLocal, TLocal), Action(TLocal))
During the parallel loop we will also use Interlock.Exchange that way we can keep the proper counter variable across multiple threads (atomic method). So here is how the method looks
public static List<string> GetAllFilesWithParallel(string root, Action<string> action)
//generic list for holding all the files
List<string> list = new List<string>();

//Count of files traversed
int i = 0;

//stack to hold all the file names
Stack<string> dirs = new Stack<string>();

//first make sure the directory provided actually exists
if (!System.IO.Directory.Exists(root))
Console.WriteLine(string.Format("The specified directory {0} could not be found", root));
return null;
//insert the root directory onto our stack (for later use)

//now we start looping
while (dirs.Count > 0)

//variables needed for this task
string currentDirectory = dirs.Pop();
string[] subDirectories = null;
string[] files = null;

//first get the subdirectories in the specified directory
subDirectories = System.IO.Directory.GetDirectories(currentDirectory);
//now get all the files in the current directory
files = System.IO.Directory.GetFiles(currentDirectory);
//add all the subdirectories onto the stack for traversal
foreach (string str in subDirectories)

//now use the Parallel.ForEach loop to get all the files in the current directory
//and add them to our generic list
Parallel.ForEach(files, () => 0, (file, loopState, localCount) =>
return (int)++localCount;
(c) =>
Interlocked.Exchange(ref i, i + c);
catch (AggregateException e)
e.Handle((ex) =>
if (ex is UnauthorizedAccessException)
// Here we just output a message and go on.
return true;
return false;
return list;

To test our method we will do this in our main method. Here we will pass it the directory we wish to search and our Action. In this case our action will be adding the file names to our Generic list. We will prompt for the directory and get the files and count them. which looks like this

static void Main(string[] args)
List<string> files = new List<string>();
Console.WriteLine("Please provide the directory you would like to count:");
string root = Console.ReadLine();

Console.WriteLine("Counting files .....");

GetAllFilesWithParallel(root, (f) => { files.Add(f); });

if(files.Count != 0)
Console.WriteLine(string.Format("Total files in {0}", files.Count()));

// Keep the console window open.

Notice we pass the method an Action, a delegate than encapsulates a method with no parameters. We go this route so we dont have to explicitly declare a delegate. So from this beginners look you can see that parallel programming is a powerful tool offered from Microsoft, and can finally allow us as programmers to keep up (somewhat anyways) with the ever changing hardware world and performance they're offering.

So this is a beginners look at the new parallel programming ability in C# 4.0. Next we will take a more intermediate look at parallel programming and how it can increase performance in your applications.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}