## Saturday, 12 January 2013

### Continuing tasks in TPL

This article will present code where a task is continued with another task in TPL. Let's look at some code for this:

class Program
{
static void Main(string[] args)
{
var task = Task.Factory.StartNew(() =>
{
Console.WriteLine("Running the first task ...");
Thread.Sleep(2000);
Console.WriteLine("First task done");
}).ContinueWith((antecendent) =>
{
Console.WriteLine("Continuing the second task ...");
Thread.Sleep(3000);
Console.WriteLine("Second task done");
}, TaskContinuationOptions.NotOnFaulted | TaskContinuationOptions.NotOnCanceled);

task.Wait();

Console.WriteLine("Press any key to continue ...");
Console.ReadKey();

}
}


A task is started with Task.Factory.StartNew and the empty lambda contains a block following that contains the code that will run the work of the first task. Now, the ContinueWith extension method is used next to continue with the next task. I have specified TaskContinuationOptions here with NotOnFaulted and NotOnCanceled, this is an overload and one does not have to specify this always. An often used continuing TaskContinuationOption is OnlyOnFaulted. Let's look last at some code that actually returns some data and gives that data to the next task continuing the work. This makes it possible to partition the work performed in much like a pipeline, one calls this kind of parallelism as dataflow parallelism.

class Program
{
static void Main(string[] args)
{
var task = Task.Factory.StartNew(() =>
{
Console.WriteLine("Running the first task ...");
Thread.Sleep(2000);
Console.WriteLine("First task done, returning some result");
return 42;
}).ContinueWith((antecendent) =>
{
Console.WriteLine("Continuing the second task ...");
Thread.Sleep(3000);
Console.WriteLine("Got result from first task: {0}", antecendent.Result);
Console.WriteLine("Second task done");
return antecendent.Result * 2;
}).ContinueWith((antecendent) => {
Console.WriteLine("Continuing the third task ...");
Thread.Sleep(3000);
Console.WriteLine("Got result from second task: {0}", antecendent.Result);
Console.WriteLine("Second task done");
return antecendent.Result * 2;
});

task.Wait();

Console.WriteLine("Final result: {0}", task.Result);

Console.WriteLine("Press any key to continue ...");
Console.ReadKey();

}


In the code above, one not only continue a task with another task, but then continue with a third task. There can be arbitrary number of tasks being chained in this way. We propagate the result from the previous example by accessing the Result of the antecendent task (previous task). This makes it possible to partition a problem into stages - a pipeline of tasks doing individual processing to create the final result, much like how a factory works. It is possible to propagate values that are not only value types such as int, but also Reference types (arbitrary complex classes). This shows that TPL is able to help you compose the total amount of work to be processed into stages in a pipeline fashion in a simple way. Keep in mind that there are more optimized way of doing this following a more advanced Producer-Consumer approach if one wishes to implement a true pipeline with TPL.