C# get results from Task.WhenAll

The C# method Task.WhenAll can run a bunch of async methods in parallel and returns when every one finished.

But how do you collect the return values?

Imagine that you have this pseudo-async-method:

private async Task<string> GetAsync(int number)
{
  return DoMagic();
}

And you wish to call that method 20 times, and then collect all the results in a list?

That is a 3 step rocket:

  1. Create a list of tasks to run
  2. Run the tasks in parallel using Task.WhenAll.
  3. Collect the results in a list
// Create a list of tasks to run
List<Task> tasks = new List<Task>();
foreach (int i=0;i<20;i++)
{
  tasks.Add(GetAsync(i));
}

// Run the tasks in parallel, and
// wait until all have been run
await Task.WhenAll(tasks);

// Get the values from the tasks
// and put them in a list
List<string> results = new List<string>();
foreach (var task in tasks)
{
  var result = ((Task<string>)task).Result;
  results.Add(result);
}

MORE TO READ:

 

Advertisement

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in General .NET. Bookmark the permalink.

3 Responses to C# get results from Task.WhenAll

  1. Michael Shedden says:

    // This is a bit nicer (and shorter) I think

    // Create a list of tasks to run
    List<Task> tasks = new List<Task>();
    foreach (int i=0;i<20;i++)
    {
    tasks.Add(GetAsync(i));
    }

    // Run the tasks in parallel, and
    // wait until all have been run
    // collecting the results at the same time
    var results = await Task.WhenAll(tasks);

    // the above is an array, but if a list is required this line could be used instead:
    // var results = (await Task.WhenAll(tasks)).ToList();

    Like

  2. Michael Shedden says:

    // This is a bit nicer (and shorter) I think – my previous posting
    // looked like the Task lost its generic parameter, string

    // Create a list of tasks and start them running
    List<Task> tasks = new List<Task>();
    foreach (int i=0;i<20;i++)
    {
    tasks.Add(GetAsync(i));
    }

    // Wait until the running tasks have been run
    // collecting the results at the same time
    var results = await Task.WhenAll(tasks);

    // the above is an array, but if a list is required this line could be used instead:
    // var results = (await Task.WhenAll(tasks)).ToList();

    Like

  3. Sergey says:

    Task.WhenAll does not run tasks!!
    But for collecting all the results in one array, it is fine.
    var results = await Task.WhenAll(tasks);

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.