Compartir a través de


.Net 4.0 : Task Parallel Library

Hi There, I am Syam Pinnaka, Dev in IAM services team at Microsoft.

.Net has provided many different ways of Asynchronous programming over various releases. The latest offering that comes with .Net 4.0 is Task Parallel Library (TPL). TPL comes with various benefits over other Async programming models like IAsyncResult or Event based models. The most notable benefits are as follows.

  1. Ability to create parent to child tasks and create complex task hierarchy of dependent tasks.
  2. Ability to Cancel or timeout tasks.
  3. Ability to Wait for one or all tasks.
  4. Continuation options for failed and succeeded tasks.
  5. Ability to catch and handle exceptions.

Exception handling with Async programming is present with both IAsyncResult and Event based models but TPL makes this looks so natural that even a new programmer can start using without much difficulty. As always lets see some example code about how to start using Task Parallel Library.

Creating a task and getting the result is just a few lines of code.

         Task<int> t1 = new Task<int>(CalcSum, 5);
        t1.Start();
        Console.WriteLine("Sum = " + t1.Result);
         private static int CalcSum(object arg)
        {
            int sum = 0, count = (int)arg;
            for (int i = 0; i < count; i++)
                sum += i;

            Console.WriteLine("In CalcSum, Sum = " + sum);

            return sum;
        }
  

Note in the above code that a new Task that returns an int is declared. The same can be applied to create a task that returns a void or a custom data type.

Assuming that the “CalcSum” returns a void, in order to  compare “Task” with “QueueUserWorkItem” the below two lines of code are equivalent.

             ThreadPool.QueueUserWorkItem(CalcSum, 5);
            Task t2 = new Task(CalcSum, 5).Start();

Task has methods like .Wait, .ContinueWith and .RunSynchronously which will be handy in certain situations.

Task status can be found with .Status or .IsFaulted and .IsCompleted and .IsCanceled.

Exception if any during the task execution can be found using .Exception.

A method that needs special attention is .ContinueWith. This can be used to Continue a task when the parent task is completed or Failed or Cancelled. Example code would look like this.

             Task<int> t1 = new Task<int>(CalcSum, 5);
            t1.Start();
            t1.ContinueWith(task => Console.WriteLine("Fault: " + task.Exception, TaskContinuationOptions.OnlyOnFaulted);
  

Similarly .ContinueWhenAll can be used to continue a task when more than one task is completed. This concept can easily be used to build a hierarchy of dependent tasks with complex structure.

In addition to Task, .Net 4.0 TPL also provides three static methods which can be used to schedule lot of tasks with independent work. These constructs are as below.

  1. System.Threading.Parallel.For; Example: Parallel.For(0, 100, i=> DoSomething() );
  2. System.Threading.Parallel.ForEach; Example: Parallel.ForEach(collection, item =>DoSomething(item));
  3. System.Threading.Parallel.Invoke; Example: Parallel.Invoke( ()=> DoSomething1(), ()=> DoSomething2());

I find both Task and Parallel very useful to address most of my common asynchronous needs without having to deal with Threading complexities. Hope you will find them useful too.

Thanks for reading and happy coding!