异步方法并行执行

时间:2019-07-12 09:25:16

标签: c# async-await

我正在尝试使用异步方法并行执行一些方法,但是这些方法不是并行执行的。

这是我的代码。

public class MyServiceAsync
{
    private long a = 10000;
    public async Task GetData()
    {
        Console.WriteLine(System.DateTime.Now.ToLongTimeString());
        var task1 = Method1();
        Console.WriteLine(System.DateTime.Now.ToLongTimeString());
        var task2 = Method2();
        Console.WriteLine(System.DateTime.Now.ToLongTimeString());
        var task3 = Method3();
        Console.WriteLine(System.DateTime.Now.ToLongTimeString());

        await task1;
        await task2;
        await task3;
        Console.WriteLine(System.DateTime.Now.ToLongTimeString());
    }

    public async Task Method1()
    {
        for (long i = 0; i < a * a; i++)
        {

        }
    }
    public async Task Method2()
    {
        for (long i = 0; i < a * a; i++)
        {

        }
    }
    public async Task Method3()
    {
        for (long i = 0; i < a * a; i++)
        {

        }
    }
}

我正在执行方法。

MyServiceAsync service1 = new MyServiceAsync();
service1.GetData().Wait();

直到method1完成,method2才执行。

谢谢 伊姆兰森

1 个答案:

答案 0 :(得分:0)

使用await Task.WhenAll(task1, task2, task3);使它们并行执行。

目前,您仍在逐个执行每个任务,以串行方式执行任务。

编辑:

演示帕纳吉奥蒂斯对“此代码中没有任务”的含义:

无任务:

class Program
{
    static void Main(string[] args)
    {
        var mainTask = MainAsync(args);
        mainTask.GetAwaiter().GetResult();

    }

    static async Task MainAsync(string[] args)
    {
        var task1 = Method1();
        var task2 = Method2();
        var task3 = Method3();

        await Task.WhenAll(task1, task2, task3);

        Console.ReadLine();
    }


    public static async Task<long> Method1()
    {
        var returnValue = 0l;
        for (long i = 0; i < 5; i++)
        {
            Console.WriteLine($"Method 1 : {i}");
            returnValue += i;
        }

        return await Task.FromResult(returnValue);
    }

    public static async Task<long> Method2()
    {
        var returnValue = 0l;
        for (long i = 0; i < 5; i++)
        {
            Console.WriteLine($"Method 2 : {i}");
            returnValue += i;
        }

        return await Task.FromResult(returnValue);
    }

    public static async Task<long> Method3()
    {
        var returnValue = 0l;
        for (long i = 0; i < 5; i++)
        {
            Console.WriteLine($"Method 3 : {i}");
            returnValue += i;
        }

        return await Task.FromResult(returnValue);
    }

}

输出:

Method 1 : 0
Method 1 : 1
Method 1 : 2
Method 1 : 3
Method 1 : 4
Method 2 : 0
Method 2 : 1
Method 2 : 2
Method 2 : 3
Method 2 : 4
Method 3 : 0
Method 3 : 1
Method 3 : 2
Method 3 : 3
Method 3 : 4

具有任务(即正确等待真正的异步操作):

class Program
{
    static void Main(string[] args)
    {
        var mainTask = MainAsync(args);
        mainTask.GetAwaiter().GetResult();

    }

    static async Task MainAsync(string[] args)
    {
        var task1 = Method1();
        var task2 = Method2();
        var task3 = Method3();

        await Task.WhenAll(task1, task2, task3);

        Console.ReadLine();
    }


    public static async Task<long> Method1()
    {
        var returnValue = 0l;
        for (long i = 0; i < 5; i++)
        {
            Console.WriteLine($"Method 1 : {i}");
            returnValue += i;
            await Task.Delay(1);
        }

        return await Task.FromResult(returnValue);
    }

    public static async Task<long> Method2()
    {
        var returnValue = 0l;
        for (long i = 0; i < 5; i++)
        {
            Console.WriteLine($"Method 2 : {i}");
            returnValue += i;
            await Task.Delay(1);
        }

        return await Task.FromResult(returnValue);
    }

    public static async Task<long> Method3()
    {
        var returnValue = 0l;
        for (long i = 0; i < 5; i++)
        {
            Console.WriteLine($"Method 3 : {i}");
            returnValue += i;
            await Task.Delay(1);
        }

        return await Task.FromResult(returnValue);
    }

}

输出:

    Method 1 : 0
    Method 2 : 0
    Method 1 : 1
    Method 3 : 0
    Method 3 : 1
    Method 1 : 2
    Method 2 : 1
    Method 3 : 2
    Method 1 : 3
    Method 2 : 2
    Method 2 : 3
    Method 1 : 4
    Method 3 : 3
    Method 3 : 4
    Method 2 : 4