Reputation: 8099
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?
Upvotes: 5
Views: 2217
Reputation: 8037
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:
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.
Put your tasks into a collection and then use await
Task.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);
Upvotes: 5