同步任务异步编程运行

Task asynchronous programming run synchronously

通过阅读以下指南 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/ 我试图创建三个应该 运行 异步并发的任务,但它们实际上是 运行 同步的。我想知道我哪里错了

Task<double?[]> rainfallGridValuesTask = Rainfall.ValuesAsync(rainfallGridValuesRepo.GetAll());  //it takes 5s
Task<double?[]> rainfallAvgValuesTask = Rainfall.AveragesAsync(rainfallAvgGridValuesRepo.GetAll());  //it takes 5s
Task<double?[][]> rainfallAnomaliesTask = Rainfall.AnomaliesAsync(rainfallGridValuesRepo.GetAll());  //it takes 5s

方法如下:

public static async Task<double?[]> Values(IQueryable<RainfallGridValue> rainfallGridValues)
{
    double?[] outputValues = new double?[108];

    System.Threading.Thread.Sleep(5000);  //Simulate the time taken by the method
    return outputValues;
}

那我尝试这样获取任务返回的值:

rainfallValueChart.Data = await rainfallGridValuesTask;
rainfallAverageChart.Data = await rainfallAvgValuesTask ;
rainfallAnomalyChart.Data = await rainfallAnomaliesTask ;

但是当我 运行 这段代码时,它在三个 Async 方法中的每一个上等待 5 秒,所以出了什么问题,我如何才能同时 运行 它们并在所有任务完成后继续?

正如评论所指出的,使用 await Task.Delay(TimeSpan.FromSeconds(5)); 而不是 System.Threading.Thread.Sleep(5000)。这将模拟在每个等待任务中进行五秒的 I/O(而不是五秒的 CPU 计算)。