Skip to main content

Posts

Showing posts from June, 2014

Extending WhenAny,WhenAll like feature to get the task as soon as it completes

While working with list of task, we only get two built in options to retrieve task info for completion. First is WhenAny and other WhenAll. WhenAny can be used when any of the given tasks gets completed first where as WhenAll notifies when all of the task gets completed. There is no option to know each task as soon as it gets completed.

I was going through Pro Asynchronous Programming with .NET book which shows better option to deal with notification of task completion efficiently.

To return the task on completion, TaskCompletionSource will be used . Through TaskCompletionSource, Task could be created and with provided inbuilt methods outcome can be handled. This can be also helpful to wrap up legacy codes as well to built up task.

Let's directly look in to core code to return task based on completion.

/// <summary> /// Order by completion for Task. /// </summary> /// <typeparam name="T">Return type</typeparam> /// <…