Using TaskFactory to manage easily dependencies between threads.

With the new TaskFactory class from .Net 4.0 it has become incredibly easy to manage dependencies between multiple tasks.
In below code sample, the TaskFactory receive 6 tasks to execute. Additional logic is added as:
- Task A will only be run when Task B and C are completed.
- Task C will only be run when Task F, E or D is completed.

If you are confused with the ‘=>’ have a look at lambda expression.

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

namespace TasksDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            UseTask demoTask = new UseTask();
            Console.ReadKey();
        }
    }

    class UseTask
    {
        public UseTask()
        {
            // Set the TaskFactory - Create and schedule objects
            var tf = new TaskFactory(
                TaskCreationOptions.AttachedToParent,
                TaskContinuationOptions.AttachedToParent);

            var F = tf.StartNew(() => DoSomething(‘F’));
            var E = tf.StartNew(() => DoSomething(‘E’));
            var D = tf.StartNew(() => DoSomething(‘D’));

            // C will be started only when F, E or D has been completed
            var C = tf.ContinueWhenAny(new Task[]{F, E, D}, tasks => DoSomething(‘C’)); 

            var B = tf.StartNew(() => DoSomething(‘B’));

            // A will only happens once B and C are completed.
            var A = tf.ContinueWhenAll(new Task[] {B, C}, tasks => DoSomething(‘A’));
        }

        private void DoSomething(char @char)
        {
            Console.WriteLine(“DoSomething called - {0}”, @char);
            Random rd = new Random();
            Thread.Sleep(rd.Next(1000, 3000));
            Console.WriteLine(“DoSomething called - {0} - DONE”, @char);
        }
    }
}

 

Isn’t that beautiful?


What am I doing? Software testing :-)

Since I moved to Zurich, two years ago, I sadly did not have the time to see most of my friends I have in Geneva. This post will hopefully explain you what I am doing in Zurich as an Engineer in Test at Microsoft. I know that even the people who know me well do


Windows Phone 7: Hands on Demo.

Interesting video (from Channel 9) showing a preview of the winPhone 7 OS. I am just a bit worried about the localized version of Bing. So far I have seen it working in the US. Does that mean that Bing will be updated for a worldwide support or that the winPhone7 will be available only


Microsoft Pivot – an innovative way of browsing information

After playing with Pivot, I have been really existed by the possibilities that Pivot offers. “Pivot makes it easier to interact with massive amounts of data in ways that are powerful, informative, and fun.” Simply speaking, Pivot is a tool that helps us visually browse collections of information. Figure 1: Item view in Pivot while


Siri, the mobile personal assistant

This mobile personal assistant looks awesome! Unfortunately, for business model reason (the application is free and make money on referrals fees whenever someone buy through the service), the service works only in the US… Interesting interview to watch with the Siri CEO and VP engineering. Still is a great application to have on your pocket


Synchronous and Asynchronous use of Delegate with C#

 For developers the quality of a program can be often express in the time used to finish the computation. With current multicore processor we have to move our thinking from a serial execution to a concurrent execution. Using delegates in an asynchronous way can force the CLR to allocate multiple threads to your computation. In


Get Arguments array from CmdLine of a WPF Application.

  This is my first post of, I hope, a series related to WPF and C#. The examples below are made with VS (Visual Studio) 2010 and .Net 4.0, for this precise example you can make it work with .Net 3.5 at least. During the life of an application we can listen for some important