Task Parallel Library in .NET 4.0 – Part 2

Nov 14th, 2010 | By | Category: Framework & Libraries

In my last post I had started a discussion on Task Parallel Library introduced as part of .NET 4.0.In this post also, we will continue with same.We will take a look at what happens when a task creates one more task during it’s execution i.e. what are nested and child tasks and how they are handled.

The following code snippet shows one nested task created by a parent task.

public void RunNestedTaskDemo()
{
     var t1 = new Task(
                         () =>
                         {
                             Console.WriteLine("Task1 Started");
                             var t11 = new Task(
                                                 () =>
                                                 {
                                                     Console.WriteLine("Task11 Started");
                                                     System.Threading.Thread.SpinWait(1000000000);
                                                     Console.WriteLine("Task11 Completed");
                                                 }
                                               );
                             t11.Start();

                             System.Threading.Thread.SpinWait(10000000);
                                                      }
                      );
     t1.Start();
     t1.Wait();
     Console.WriteLine("Task1 Completed");

}

The output of this program will be:

Task1 Started
Task11 Started
Task1 Completed
Task11 Completed

So task t1 creates and starts t11, then t1 completes and finally t11 completes.Here t11 is the nested task as it’s parent does not wait for it’s completion.

public void RunChildTaskDemo()
{
     var t1 = new Task(
                         () =>
                         {
                             Console.WriteLine("Task1 Started");
                             var t11 = new Task(
                                                 () =>
                                                 {
                                                     Console.WriteLine("Task11 Started");
                                                     System.Threading.Thread.SpinWait(1000000000);
                                                     Console.WriteLine("Task11 Completed");
                                                 },TaskCreationOptions.AttachedToParent
                                               );
                             t11.Start();
                             System.Threading.Thread.SpinWait(10000000);

                         }
                      );
     t1.Start();
     t1.Wait();
     Console.WriteLine("Task1 Completed");

}

Here to create the nested task we have used the following constructor:

public Task(Action action,TaskCreationOptions creationOptions)

The TaskCreationOptions enumeration has a value AttachedToParent which indicates that this task will be attached to parent task and it’s parent will wait for it’s completion as evident from the output shown below:

Task1 Started
Task11 Started
Task11 Completed
Task1 Completed

This type of nested tasks are referred to as the child tasks.

In our next post we will discuss about task cancellation options available in Task Parallel Library.


Kick It on DotNetKicks.com
Tags: , , ,
  • Netmajor

    Great explanation of TPL! Thank You very much:) I will wait for next parts ;)

  • http://codingndesign.com/blog/?p=202 Task Parallel Library in .NET 4.0 – Part 3 – Coding N Design

    [...] my last post we had completed our discussion on the nested and child tasks i.e what happens when another task is [...]