This is a mirror of official site: http://jasper-net.blogspot.com/

Task.WhenAll

| Sunday, September 23, 2012
When Task.WhenAll met the Task.WhenAny…
There are two methods which can be used for awaiting an array of tasks in non blocking manner: Task.WhenAll and Task.WhenAny.

It is quite obvious how they work:
  • WhenAll completes when every task is completed
  • WhenAny when any of the task is completed.

I needed today something which is a mix of those two and I’ve came up with something which completes when all is awaited but also provides hook for me to respond on awaits of individual tasks.

I’ve called my little extension: Task.WhileAll.

Extension method

Here's the complete implementation

public static class TaskExtensions 
{
    public static async Task<IList<T>> WhileAll<T>(this IList<Task<T>> tasks, IProgress<T> progress) 
    {
        var result = new List<T>(tasks.Count);
        var done = new List<Task<T>>(tasks);
        while(done.Count > 0) 
        {
            await Task.WhenAny(tasks);
            var spinning = new List<Task<T>>(done.Count - 1);
            for(int i = 0; i < done.Count; i++) 
            {
                if(done[i].IsCompleted) 
                {
                    result.Add(done[i].Result);
                    progress.Report(done[i].Result);
                } else {
                    spinning.Add(done[i]);
                }
            }
 
            done = spinning;
        }
 
        return result;
    }
}

Read more: .NET AND ME
QR: Inline image 1

Posted via email from Jasper-net

0 comments: