简体   繁体   中英

How to call multiple methods parallelly by using async programming in C#

public class Program
{
    public static void Main(string[] args)
    {
        Start();
        Console.ReadLine();
    }

    private static async Task Start()
    {
        var m1 = method1();
        var m2 = method2();

        await Task.WhenAll(m1, m2);
    }


    private static async Task method1()
    {
        Console.WriteLine("Method1 - Start");
        Thread.Sleep(1000);
        Console.WriteLine("Method1 - End");
    }
    private static async Task method2()
    {
        Console.WriteLine("Method2 - Start");
        Thread.Sleep(1000);
        Console.WriteLine("Method2 - End");
    }
}

The above code returning below out

Method1 - Start
Method1 - End
Method2 - Start
Method2 - End

I want an output like

Method1 - Start
Method2 - Start
Method1 - End
Method2 - End

how to achieve that basically how to run async methods in parallel

Option A - with Task.Delay

public class Program
{
    public static async Task Main()
    {
        await Start();
        Console.ReadLine();
    }

    private static async Task Start()
    {
        var m1 = method1();
        var m2 = method2();

        await Task.WhenAll(m1, m2);
    }

    private static async Task method1()
    {
        Console.WriteLine("Method1 - Start");
        await Task.Delay(1000);
        Console.WriteLine("Method1 - End");
    }

    private static async Task method2()
    {
        Console.WriteLine("Method2 - Start");
        await Task.Delay(1000);
        Console.WriteLine("Method2 - End");
    }
}

Option B - with Task.Run

public class Program
{
    public static async Task Main()
    {
        await Start();
        Console.ReadLine();
    }

    private static async Task Start()
    {
        var m1 = Task.Run(() => method1());
        var m2 = Task.Run(() => method2());

        await Task.WhenAll(m1, m2);
    }

    private static void method1()
    {
        Console.WriteLine("Method1 - Start");
        Thread.Sleep(1000);
        Console.WriteLine("Method1 - End");
    }

    private static void method2()
    {
        Console.WriteLine("Method2 - Start");
        Thread.Sleep(1000);
        Console.WriteLine("Method2 - End");
    }
}

Or you can use Task.Yield() .

public class Program
{
    public static void Main(string[] args)
    {
        Start();
        Console.ReadLine();
    }

    private static async Task Start()
    {
        var m1 = method1();
        var m2 = method2();

        await Task.WhenAll(m1, m2);
    }


    private static async Task method1()
    {
        await Task.Yield();
        Console.WriteLine("Method1 - Start");
        Thread.Sleep(1000);
        Console.WriteLine("Method1 - End");
    }
    private static async Task method2()
    {
        await Task.Yield();
        Console.WriteLine("Method2 - Start");
        Thread.Sleep(1000);
        Console.WriteLine("Method2 - End");
    }
}

This one is a bit "more parallel" than using Task.Delay() because it immediately yields back an uncomplete task, but with delay the "asynchronicity" only happens when you reach that line of code. If you have long running sync code before the delay, that will delay the execution of subsequent methods (in this case method2).

Edit

a more detailed explanation on When would I use Task.Yield()?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM