AK_
AK_

Reputation: 8099

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?

Upvotes: 5

Views: 2217

Answers (1)

Jon Senchyna
Jon Senchyna

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:

  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);

Upvotes: 5

Related Questions