Parallel Programming Using New TPL Library in .Net 4.0

These days nearly all computers are assembled with multicore processors. Now the time is different from when you had simple single-core processors processing a single instruction at a time of your program. If you are creating an application that has to be installed on a multicore CPU machine, then the application should be able to utilize as many CPUs as it can to provide improved performance. But to develop such an application you need to be quite skilled in multithreaded programming techniques.
 
With the .Net 4.0, you are provided with the brand new parallel programming language library called "Task Parallel Library" (TPL). Using the classes in the System.Threading.Tasks namespace, you can build fine-grained, scalable parallel code without having to work directly with threads.
 

The Task Parallel Library API

 
The primary class of the TPL is System.Threading.Tasks.Parallel. This class provides you a number of methods that allow you to iterate over a collection of data, (specifically which implements the IEnumerable<T>) in a parallel fashion. Main two static methods of this class are Parallel.For() and Parallel.ForEach(), each of which defines numerous overloaded versions.
 
Let's create a small search application in WindowsForms:
 
Create a simple form like below:
 
The Task Parallel Library API
 
Now for the click event handler of the Search Button write the code below:
  1. private void btnSearch_Click(object sender, EventArgs e)  
  2. {  
  3.     DateTime startTime = DateTime.Now;  
  4.     DateTime endTime;  
  5.     string[] dirs = Directory.GetDirectories(path);  
  6.     List<string> lstFiles = new List<string>();  
  7.     foreach (string dir in dirs)  
  8.     {  
  9.         this.Text = "Searching " + dir;  
  10.         try  
  11.         {  
  12.             lstFiles.AddRange(Directory.GetFiles(dir, "*.doc*", SearchOption.AllDirectories));  
  13.         }  
  14.           catch(Exception ex)  
  15.         {  
  16.             continue;  
  17.         }  
  18.     }  
  19.     endTime = DateTime.Now;  
  20.     TimeSpan ts = endTime.Subtract(startTime);  
  21.     MessageBox.Show("Search Complete!! \n Time elapsed:"+ts.Minutes+":"+ts.Seconds+":"+ts.Milliseconds);  
  22. }   
OUTPUT:
The Task Parallel Library API Output
 
Now write the same logic using the Parallel.ForEach() function of System.Threading.Tasks
  1. private void btnSearch_Click(object sender, EventArgs e)  
  2. {  
  3.     DateTime startTime = DateTime.Now;  
  4.     DateTime endTime;  
  5.     string[] dirs = Directory.GetDirectories(path);  
  6.     List<string> lstFiles = new List<string>();  
  7.   
  8.     Parallel.ForEach(dirs, dir =>  
  9.     {  
  10.         try  
  11.         {  
  12.             lstFiles.AddRange(Directory.GetFiles(dir, "*.doc*", SearchOption.AllDirectories));  
  13.         }  
  14.          catch  
  15.         {  
  16.              // do nothing  
  17.         }  
  18.     });  
  19.   
  20.     endTime = DateTime.Now;  
  21.     TimeSpan ts = endTime.Subtract(startTime);  
  22.     MessageBox.Show("Search Complete!! \nFiles Found:"+lstFiles.Count+"\n Time elapsed:"+ts.Minutes+":"+ts.Seconds+":"+ts.Milliseconds);  
OUTPUT
 
The Task Parallel Library API
 
That is a little faster than the previous one!
 
But here you must be facing a problem. Have you tried writing something during this operation on the UI? I don't if you could do that cause UI was freezing because the Main thread is waiting for the operation to complete that is executing in a Synchronous manner.
 
Let us now use a few more powerful functionalities of System.Threading.Tasks to make the UI interactive while work is being done.
 
Place all the code inside a function; say SearchDirectory().
  1. private void SearchDirectory()  
  2. {  
  3.     DateTime startTime = DateTime.Now;  
  4.     DateTime endTime;  
  5.     string[] dirs = Directory.GetDirectories(path);  
  6.     List<string> lstFiles = new List<string>();  
  7.   
  8.     Parallel.ForEach(dirs, dir =>  
  9.     {  
  10.         try  
  11.         {  
  12.             lstFiles.AddRange(Directory.GetFiles(dir, "*.doc*", SearchOption.AllDirectories));  
  13.         }  
  14.         catch  
  15.         {  
  16.              // do nothing  
  17.         }  
  18.     });  
  19.   
  20.     endTime = DateTime.Now;  
  21.     TimeSpan ts = endTime.Subtract(startTime);  
  22.     MessageBox.Show("Search Complete!! \nFiles Found:" + lstFiles.Count + "\n Time elapsed:" + ts.Minutes + ":" + ts.Seconds + ":" + ts.Milliseconds);  
  23. }  
To make the UI you can use async delegates but this time we're going to use a little easier approach than writing the whole code to make an Async call.
  1. private void btnSearch_Click(object sender, EventArgs e)  
  2. {  
  3.     //Start a task to process a file  
  4.     Task.Factory.StartNew(() => SearchDirectory());  
The Factory property of the Task Class returns a TaskFactory object. When you call its StartNew() method, you pass in an Action<T> delegate (Here lambda expression is hiding the expression). Now your UI will be able to receive input and will not block.
 
OUTPUT
 
The Task Parallel Library API
 

Including Cancellation Request for a background process

 
The Parallel.Foreach() and Paralled.For() both support the cancellation through the Cancellation tokens. When you invoke the methods on Parallel, you can pass in a ParallelOptions object, which in turn contains a CalcellationTokenSource object.
 
To add this functionality in your application, first of all, define a new private member variable in your form derived from a class of time CancellationTokenSource named cancelToken:
  1. //Cancellation token for canceling the invoke  
  2. private CancellationTokenSource cancelTOken = new CancellationTokenSource();  
To cancel the task just add a cancel button and write a single line in it:
  1. cancelToken.Cancel(); 
Add the Parallel option instance to store the Cancellation Token:
  1. ParallelOptions parOpts = new ParallelOptions();  
  2. parOpts.CancellationToken = cancelToken;  
  3. parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;  
  4.    
  5. try  
  6. {  
  7.     Parallel.ForEach(dirs, dir =>  
  8.     {  
  9.         parOpts.CancellationToken.ThrowIfCancellationRequested();  
  10.         try  
  11.         {  
  12.             lstFiles.AddRange(Directory.GetFiles(dir, "*.doc*", SearchOption.AllDirectories));  
  13.         }  
  14.         catch  
  15.         {  
  16.             // do nothing  
  17.         }  
  18.     });  
  19.     endTime = DateTime.Now;  
  20.     TimeSpan ts = endTime.Subtract(startTime);  
  21.     MessageBox.Show("Search Complete!! \nFiles Found:" + lstFiles.Count + "\n Time elapsed:" + ts.Minutes + ":" + ts.Seconds + ":" + ts.Milliseconds);  
  22. }  
  23. catch (OperationCanceledException ex)  
  24. {  
  25.     MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  26. }  
Within the scope of the looping logic, you make a call to ThrowIfCancellationRequested () on the token, which will ensure that if the user clicks the cancel button, all threads will stop and you will be notified via a runtime exception.
 
I hope you enjoyed reading this. Please comment if you liked it and spread the word about it.