Last active
December 13, 2015 20:19
-
-
Save v2m/4969559 to your computer and use it in GitHub Desktop.
Execute implementation
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 IEnumerable<Row> Execute(IEnumerable<Row> rows) | |
{ | |
var blockingCollection = new BlockingCollection<Row>(); | |
var count = _operations.Count; | |
if (count == 0) | |
{ | |
yield break; | |
} | |
var tasks = _operations.Select(currentOp => | |
Task.Factory.StartNew(() => | |
{ | |
try | |
{ | |
foreach (var row in currentOp.Execute(null)) | |
{ | |
blockingCollection.Add(row); | |
} | |
} | |
finally | |
{ | |
if (Interlocked.Decrement(ref count) == 0) | |
{ | |
blockingCollection.CompleteAdding(); | |
} | |
} | |
} | |
) | |
).ToArray(); | |
foreach (var row in blockingCollection.GetConsumingEnumerable()) | |
{ | |
yield return row; | |
} | |
Task.WaitAll(tasks); // raise any exception that were raised during execution | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great. Thanks