## Saturday, 12 January 2013


class Program
{
static void Main(string[] args)
{
{
}).ContinueWith((antecendent) =>
{

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

}
}


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)
{
{
Console.WriteLine("First task done, returning some result");
return 42;
}).ContinueWith((antecendent) =>
{
Console.WriteLine("Got result from first task: {0}", antecendent.Result);
return antecendent.Result * 2;
}).ContinueWith((antecendent) => {
Console.WriteLine("Got result from second task: {0}", antecendent.Result);
return antecendent.Result * 2;
});