Friday, March 22, 2013

c# - Managed Parallelism,Threading using Task Parallel Library

One of my recent task was to migrate the .Net 2.0 code for processing a few thousand files.The original code used Managed Threading using ThreadPool. I wanted to update this code using .Net4.0 Task Parallel Library's Parallel.Foreach for this task.(Source: http://msdn.microsoft.com/en-us/library/ff477033.aspx)


Below is a very very trivial example.

In the code below I am getting all the folders in a given directory and processing them in parallel using Parallel.Foreach. I am using Environment.ProcessorCount(Source: http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx ) to get the total number of core processors on my server to control the total number of cores being used for processing.In the first example I am using all the 8 cores on my server.

using System;
using System.Threading.Tasks;
using System.IO;

namespace ManagedParallelism
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                DirectoryInfo oDirectoryInfo = new DirectoryInfo(@"C:\MyFolder");
                Parallel.ForEach(oDirectoryInfo.GetDirectories(), new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, (oDirectory) =>
                {
                        Console.WriteLine("TIMESTAMP: {0} INFO: PROCESSING DIRECTORY:{1}", DateTime.Now, oDirectory.Name);
                        ProcessDirectory(oDirectory);
                        Console.WriteLine("TIMESTAMP: {0} INFO: COMPLETED DIRECTORY:{1}", DateTime.Now, oDirectory.Name);
                        
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            Console.Read();
        }
        /// <summary>
        /// Function to process each of the sub directories
        /// </summary>
        /// <param name="oDirectory"></param>
        private static void ProcessDirectory(DirectoryInfo oDirectory)
        {
            //ToDo: Add code to process each sub directory in the Raw Files Folder
            //throw new NotImplementedException();
        }
    }
}
















By changing the above code to 

Parallel.ForEach(oDirectoryInfo.GetDirectories(), new ParallelOptions { MaxDegreeOfParallelism = (Environment.ProcessorCount - 4)}

parallelism can be limited to 4 cores.This value would ideally come from a configuration file.
















Task Parallel Library does make threading easy !

No comments: