I am using SignalR. A function on a hub often returns a task. Now I have a function that will add a connection to a group of groups. I would like to return a task that represents all these tasks.
I found a great feature for this: Task.WhenAll. However, this is a new feature in .NET 4.5, and I still stick with .NET 4.
So I decided to write my own version until we move on to .NET 4.5. As the question of multithreading often arises (e.g. thread pool material), I'm not sure if my implementation is correct:
public static Task WhenAll(IEnumerable<Task> tasks) { return Task.Factory.StartNew(() => Task.WaitAll(tasks.ToArray())); }
Functionally this works, I think, but can I get an additional blocked thread for a new task? Or is it inevitable?
Edit: This is how I will use it with SignalR:
public static Task Add(this IGroupManager groupManager, string connectionId, IEnumerable<string> groups) { return WhenAll(groups.Select(group => groupManager.Add(connectionId, group))); }
source share