同时运行多个等待

时间:2016-04-28 07:04:34

标签: c# asynchronous windows-8.1-universal

我的代码看起来像这样:

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方法完成时更新。无论如何,我想在这里实现的目标是更快地获得结果。如果我一个接一个地运行这些方法,它们会等到上一个完成,但如果它们同时运行,第三个方法会更快地返回结果,对吧?所以我的问题是 - 这可能吗?如果是,那怎么样?

6 个答案:

答案 0 :(得分:5)

我认为这就是你要找的东西:

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

使用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"

答案 3 :(得分:0)

因此,您不想对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就会被设置,并且他们不会等到前一个完成。

答案 4 :(得分:0)

  

我希望所有等待操作同时运行,因此所有TextBlocks会同时更新或更少。或者可能不是在同一时间 - 每个TextBlock都会在相应的await方法完成时更新。

然后定义一些新方法:

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;

答案 5 :(得分:-1)

只需将等待的创建与实际的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";

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