Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite multi-thread wait using async/await

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 ?

like image 357
Andomar Avatar asked Oct 03 '12 18:10

Andomar


2 Answers

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

like image 170
Reed Copsey Avatar answered Oct 01 '22 08:10

Reed Copsey


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.

like image 34
Andomar Avatar answered Oct 01 '22 07:10

Andomar