运行 多个同时等待

Run multiple await at the same time

我的代码看起来像这样:

firstList = await GetFirstListFilesAsync();
textBlock1.Text = "found " + firstList.Count + " first list's results";
secondList = await GetSecondListFilesAsync();
textBlock2.Text = "found " + secondList.Count + " second list's results";
thirdList = await GetThirdListFilesAsync();
textBlock3.Text = "found " + thirdList.Count + " third list's results"

现在它获取第一个列表,设置第一个 TextBlock 文本,然后获取第二个列表,设置第二个 TextBlock 文本,然后获取第三个列表并设置第三个 TextBlock 文本。但我希望所有等待操作都会同时 运行,所以所有 TextBlock 都会或多或少地同时更新。或者可能不同时 - 每个 TextBlock 都会随着相应的 await 方法完成而更新。无论如何,我想在这里实现的是更快地获得结果。如果我一个一个 运行 那些方法,它们会等到前一个方法完成,但如果它们同时 运行 所有方法,第三个方法会 return 结果更快,对吧?所以我的问题是——这可能吗?如果是,那么如何?

您可以尝试将工作转移到 Task 的集合中,并使用 Task.WaitAll 加入线程。

使用 david 的答案或将所有等待放在一起然后像这样调用设置的文本值一样简单:

firstList = await GetFirstListFilesAsync();
secondList = await GetSecondListFilesAsync();
thirdList = await GetThirdListFilesAsync();

textBlock1.Text = "found " + firstList.Count + " first list's results";

textBlock2.Text = "found " + secondList.Count + " second list's results";

textBlock3.Text = "found " + thirdList.Count + " third list's results"

我想这就是您要找的:

var tasks = new Task<MyMethodReturnObject>[3];
tasks[0] = GetFirstListFilesAsync();
tasks[1] = GetSecondListFilesAsync();
tasks[2] = GetThirdListFilesAsync();

// At this point, all three tasks are running at the same time.

// Now, we await them all.
await Task.WhenAll(tasks);

// Get an individual result, where result is MyMethodReturnObject
var myText = tasks[0].Result;

// Loop through results
var results = tasks.Select(o => o.Result).ToList();
foreach (var result in results)
{
    Console.Write(result);
}

来源:http://blog.stephencleary.com/2012/02/async-and-await.html

只需将等待对象的 创建 与它们的实际 await 分开:

var firstWait = GetFirstListFilesAsync();
var secondWait = GetSecondListFilesAsync();
var thirdWait = GetThirdListFilesAsync();
firstList = await firstWait;
textBlock1.Text = "found " + firstList.Count + " first list's results";
secondList = await secondWait ;
textBlock2.Text = "found " + secondList.Count + " second list's results";
thirdList = await thirdWait ;
textBlock3.Text = "found " + thirdList.Count + " third list's results";

在第一项完成之前,这仍然不会显示任何结果,但确实允许并行取得进展。如果你确实想处理每个 as 它在一个函数内完成,它会更复杂,因为你必须 WaitAny 在 awaitables 的集合上,然后确定(手动) 哪些已经完成,从集合中移除那些并循环直到所有完成。

所以您不想对 await 块进行分组,您想要指定它们完成时会发生什么。

GetFirstListFilesAsync().ContinueWith(task => {
   textBlock1.Text = "found " + task.Result.Count + " first list's results";
}); 
GetSecondListFilesAsync().ContinueWith(task => {
   textBlock2.Text = "found " + task.Result.Count + " second list's results";
}); 
GetThirdListFilesAsync().ContinueWith(task => {
   textBlock3.Text = "found " + task.Result.Count + " third list's results";
});

这种方式将是最快的方式,因为一旦 Task 完成,您的 textBlock.Text 将被设置,他们不会等待前一个完成。

I would like that all await operations would run simultaneously, so all TextBlocks would update more or less at the same time. Or maybe not at the same time - each TextBlock would update as the corresponding await method finishes.

然后定义几个新方法:

private async Task<TFirst> GetFirstListFilesAndDisplayAsync()
{
  var firstList = await GetFirstListFilesAsync();
  textBlock1.Text = "found " + firstList.Count + " first list's results";
}

private async Task<TSecond> GetSecondListFilesAndDisplayAsync()
{
  var secondList = await GetSecondListFilesAsync();
  textBlock2.Text = "found " + secondList.Count + " second list's results";
}

private async Task<TThird> GetThirdListFilesAndDisplayAsync()
{
  var thirdList = await GetThirdListFilesAsync();
  textBlock3.Text = "found " + thirdList.Count + " third list's results";
}

然后同时调用它们:

var firstTask = GetFirstListFilesAndDisplayAsync();
var secondTask = GetSecondListFilesAndDisplayAsync();
var thirdTask = GetThirdListFilesAndDisplayAsync();

await Task.WhenAll(firstTask, secondTask, thirdTask);

firstList = await firstTask;
secondList = await secondTask;
thirdList = await thirdTask;