Created
August 6, 2020 00:29
-
-
Save mmoroni/a0da5a18fdb859c4a59a070e8ad64fa9 to your computer and use it in GitHub Desktop.
Code Review!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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