Transform IEnumerable> asynchronously by awaiting each task

前端 未结 1 926
旧巷少年郎
旧巷少年郎 2020-12-11 00:05

Today I was wondering how to transform a list of Tasks by awaiting each of it. Consider the following example:

private static void Main(string[] args)
{
             


        
相关标签:
1条回答
  • 2020-12-11 01:09

    What about this:

    await Task.WhenAll(tasks1);
    var gainStrings = tasks1.Select(t => t.Result).ToList();
    

    Wait for all tasks to end and then extract results. This is ideal if you don't care in which order they are finished.

    EDIT2: Even better way:

    var gainStrings = await Task.WhenAll(tasks1);
    
    0 讨论(0)
提交回复
热议问题