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:
var tasks = rowlist.Select(row => Task.Run(() => DoWork(row)));
var results = (await Task.WhenAll(tasks)).ToList();
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 (via await Task.WhenAll
), then pull out the results at once (since you know they're all done).
Based on Reed Copsey, Servy and Stephen Cleary's answer and comments I've progressed to this solution:
var results = rowlist
.Select(row => Task.Run(() => DoWork(row)))
.ToList()
.Select(t => t.Result);
The second Select
will wait on the tasks to finish. I've added a ToList()
in between to prevent streaming, but it seems to work without that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With