Asynchronous function above the list

I have a function that looks like this:

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

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

+5
source share
1 answer

Here's an MSDN article on using async-awaitmultilpe for parallel processing of tasks. And here is another one that is specifically designed for a set of tasks.

In short, you can do one of the following:

  • Run all of your tasks, and then awaiteach of them. All of them will work in parallel, and your program will continue as soon as they are all completed.

  • Put your tasks in the collection and then use await Task.WhenAllto wait for several tasks.

An example of the second method will 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);
+5
source

All Articles