Call async method from another async method

  Kiến thức lập trình

I am trying to run an asynchronous method from another asynchronous method and make sure that both of them complete successfully. Like in the example below I am adding a delay of 1000ms in Function1() and a delay of 3000ms in Function2(), meaning the Main() task should complete first. Also, I am using .Net6.0 to able to run this snippet.

using System;
using System.Threading.Tasks;
using System.Threading;
                    
public class Program
{
    static async Task Main(string[] args)
    {
        var inst = new SomeClass();
        // inst.Functon1(); // Method is called but not waited till its completion
        await inst.Functon1(); // Seems this blocks this Main method itself
        Console.WriteLine("finished");
    }
}

public class SomeClass
{
    public async Task Functon1(){
        await Task.Delay(1000); // should have await to block
        Console.WriteLine("SomeClass In Func1");
        await Functon2();
    }
    
    private async Task Functon2(){
        await Task.Delay(3000);
        Console.WriteLine("SomeClass In Func2");
        await Functon3();
    }
    
    private async Task Functon3(){
        Console.WriteLine("SomeClass In Func3");
    }
}

Output should be like below, in the exact same order is the expectation.

finished
SomeClass In Func1
SomeClass In Func2
SomeClass In Func3

PS – I tried calling inst.Functon1() with .ConfigureAwait(true) and ConfigureAwait(false) but it made little difference in an async method.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT