Skip to content

Instantly share code, notes, and snippets.

@mmoroni
Created August 6, 2020 00:29
Show Gist options
  • Save mmoroni/a0da5a18fdb859c4a59a070e8ad64fa9 to your computer and use it in GitHub Desktop.
Save mmoroni/a0da5a18fdb859c4a59a070e8ad64fa9 to your computer and use it in GitHub Desktop.
Code Review!
public class ParallelRunner<T>
{
private List<Task<List<T>>> Tasks { get; } = new List<Task<List<T>>>();
/// <summary>
/// Queues the specified work to run
/// </summary>
public void Run(Task<List<T>> task)
{
Tasks.Add(task);
}
/// <summary>
/// Wait for all enqueued work to finish and returns a list of <see cref="T:System.Threading.Tasks.Task`1" />
/// </summary>
public async Task<IEnumerable<T>> Result()
{
var result = await Task.WhenAll(Tasks);
return result.SelectMany(o => o);
}
public void Run(Task<T> task)
{
Tasks.Add(task.ContinueWith(t => new List<T>{t.Result}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment