Creating a task in a task

Exploring what happens when creating a task from within a task

Home DailyDrop

Daily Knowledge Drop

By default, when creating a child task from within a parent task, the child task has no dependency on the parent task - they are unrelated and will be run independently.

If a parent task is require to wait on the child task(s) to complete, then the TaskCreationOptions.AttachedToParent option is to be used when creating the child task.


Single Task

First we'll look at an example of just a single task being created:

var parentTask = new Task(() =>
{
    Console.WriteLine("Main task started");
    Console.WriteLine("Main task finished");
});

parentTask.Start();
parentTask.Wait();

Console.WriteLine("Application closing");

There is no much going on here. A parent task is created (which does nothing but print to the console), started and the code waits until it finishes.

The output is as follows:

Main task started
Main task finished
Application closing

Detached child

Next let's create a child task in the parent task, using the default settings, and see what is output:

var parentTask = new Task(() =>
{
    Console.WriteLine("Main task started");
    var childTask = new Task(() =>
    {
        Console.WriteLine("Sub task started");
        Thread.Sleep(1000);
        Console.WriteLine("Sub task finished");
    });

    childTask.Start();

    Console.WriteLine("Main task finished");
});

parentTask.Start();
parentTask.Wait();

Console.WriteLine("Application closing");

Here, from within the parent task, a child task is created (which writes to the console, but also sleeps for 1 second), and is started.

The output for the above is as follows:

Main task started
Main task finished
Application closing

No information from the child task is output! - this is because the code is only waiting on the parent task and there is no dependency between the the child and the parent task.

The child task doesn't have a chance to output to the console before the parent task finishes processing and the application closes.


Attached child

To wait on all child tasks as well, the child tasks need to be attached to the parent:

var parentTask = new Task(() =>
{
    Console.WriteLine("Main task started");
    var childTask = new Task(() =>
    {
        Console.WriteLine("Sub task started");
        Thread.Sleep(1000);
        Console.WriteLine("Sub task finished");
    }, TaskCreationOptions.AttachedToParent);

    childTask.Start();

    Console.WriteLine("Main task finished");
});

parentTask.Start();
parentTask.Wait();

Console.WriteLine("Application closing");

When the child task is created, on line 9 the TaskCreationOptions.AttachedToParent option is specified. Now, when waiting on the parent task, all child tasks will automatically be waited on as well.

The output is now as follows:

Main task started
Main task finished
Sub task started
Sub task finished
Application closing

Notes

When creating tasks in tasks - its very important to know and understand how the tasks are created and how they interact (or don't) with each other, as it can drastically change how the application operates.

The above is just one example on how the options of a task can be changed to modify its behavior.


References

Attached and Detached Child Tasks

Daily Drop 56: 20-04-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net task parallel