'Async Task calls inside a function with emphasis on execution order and dependencies

I'm treading into the weeds as a newbie on async dev. I wanted to understand the best practice of handling the following situation. I have 3 sets of tasks to execute. Each subsequent set of tasks relies on the previous set completing before it runs (except for the first set, of course). In one case, a set of tasks needs the output from the prev set of tasks. In another case, I just need a set of tasks to complete before executing the next set.

Please let me know if I have the proper flow in my code below to achieve the desired behavior. Please suggest a better method if I'm not on the right track. Thanks in advance!


public static Task Parent
{
    return Task.Run(async () =>
    {
         //I need the result of these 2 tasks for my next 2 tasks
         Task<string> sDependentTask1 = DoStuff1();
         Task<string> sDependentTask2 = DoStuff2();

         await Task.WhenAll(sDependentTask1, sDependentTask2);

         string task1Output = await sDependentTask1;
         string task2Output = await sDependentTask2;

         //needs the output of the previous 2 tasks before these 2 run
         Task nextTask1 = AnotherTask1(task1Output);
         Task nextTask2 = AnotherTask2(task2Output);

         await Task.WhenAll(nextTask1, nextTask2);

         //these final 2 tasks cannot run until nextTask1 and nextTask2 is complete
         await FinalTask1();
         await FinalTask2();

    });
}

public static Task<string> DoStuff1()
{
    return Task.Run(() =>
    {
         return "stuff 1";
    });
}

public static Task<string> DoStuff2()
{
    return Task.Run(() =>
    {
         return "stuff 2";
    });
}

public static async Task AnotherTask1(string task1Output)
{
    await FunctionCallAsync(task1Output);
}

public static async Task AnotherTask2(string task2Output)
{
    await FunctionCallAsync(task2Output);
}

public static async Task FinalTask1()
{
    await AnotherCallAsync();
}

public static async Task FinalTask2()
{
    await AnotherCallAsync();
}


Solution 1:[1]

Looks pretty much like what you want. But you can certainly simplify:

public static async Task Parent()
{
    //I need the result of these 2 tasks for my next 2 tasks
    Task<string> sDependentTask1 = DoStuff1();
    Task<string> sDependentTask2 = DoStuff2();

    string task1Output = await sDependentTask1;
    string task2Output = await sDependentTask2;

    //needs the output of the previous 2 tasks before these 2 run
    Task nextTask1 = AnotherTask1(task1Output);
    Task nextTask2 = AnotherTask2(task2Output);

    await Task.WhenAll(nextTask1, nextTask2);

    //these final 2 tasks cannot run until nextTask1 and nextTask2 is complete
    await FinalTask1();
    await FinalTask2();
}

public static async Task<string> DoStuff1()
{
    return "stuff 1";
}

public static async Task<string> DoStuff2()
{
    return "stuff 2";
}

public static async Task AnotherTask1(string task1Output)
{
    await FunctionCallAsync(task1Output);
}

public static async Task AnotherTask2(string task2Output)
{
    await FunctionCallAsync(task2Output);
}

public static async Task FinalTask1()
{
    await AnotherCallAsync();
}

public static async Task FinalTask2()
{
    await AnotherCallAsync();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jesse C. Slicer