We have a webservice function that runs a list of tasks in the background. When all tasks are complete, it returns. I’ve revisited the function for C# 5. I’ve managed to get the following snippet working:
var result = new List<int>();
var tasks = new List<Task<int>>();
foreach (var row in rowlist)
tasks.Add(Task.Factory.StartNew(() => DoWork(row)));
foreach (var task in tasks)
result.Add(task.Result);
The task.Result waits for the individual task to be done. I’ve tested this by adding a three second delay to the DoWork method and verifying that it finishes in less than 5 seconds, for a list of 10 tasks.
Now I’ve tried to rewrite the function using the new async / await syntax. But I can’t seem to get it to work. If it compiles, it runs synchronously, as verified by adding a wait.
Any ideas on how you can rewrite the above snippet using async / await ?
I would write this as:
Basically, you still create the tasks the way you were previously (without using
await), as you want them to all start immediately. You can then wait for all of them to complete asynchronously (viaawait Task.WhenAll), then pull out the results at once (since you know they’re all done).