A Task May Start Child Tasks

1. How to create children tasks attached to its parent task?

By default, a top-level task that creates internal tasks doesn’t have relationships to the tasks that create. To make a task which contains children tasks isn’t considered as completed until its children tasks have finished running, we can create children tasks attached to its parent task.

static void AttachedToParentTest()


    Task<int[]> mainTask = new Task<int[]>(() =>


            int[] result = new int[3];

            new Task(() => result[0] = GetResult(100)

                , TaskCreationOptions.AttachedToParent).Start();

            new Task(() => result[1] = GetResult(200)

                , TaskCreationOptions.AttachedToParent).Start();

            new Task(() => result[2] = GetResult(300)

                , TaskCreationOptions.AttachedToParent).Start();

            return result;


    Trace.WriteLine(“Main thread Id:” + Thread.CurrentThread.ManagedThreadId.ToString());


    Task continueWithTask = mainTask.ContinueWith(tasks => Array.ForEach(tasks.Result,

    result =>



        Trace.WriteLine(“ContinueWith thread Id:” + Thread.CurrentThread.ManagedThreadId.ToString());