Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async function over a list

I Have a function that looks like this:

public async Task<decimal> GoToWeb(string Sym){}

what's the best way to call it over a list of strings?

like image 950
AK_ Avatar asked Aug 02 '12 18:08

AK_


1 Answers

Here's an article from MSDN on using async-await to process multilpe tasks in parallel. And here's another that specifically addresses a collection of tasks.

In short, you can do one of the following:

  1. Start all of your tasks and then await each of them. They will all run in parallel and your program will continue once they all complete.

  2. Put your tasks into a collection and then use awaitTask.WhenAll to wait for multiple tasks.

An example of the second method would be as follows:

List<string> Syms = ... // Create your list of strings
IEnumerable<Task<decimal>> tasks = from Sym in Syms select GoToWeb(Sym);
decimal[] results = await Task.WhenAll(tasks);
like image 197
Jon Senchyna Avatar answered Nov 07 '22 21:11

Jon Senchyna