Last active
December 15, 2015 18:09
-
-
Save trullock/5301694 to your computer and use it in GitHub Desktop.
This file contains 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
this.cancellationTokenSource = new CancellationTokenSource(); | |
var po = new ParallelOptions | |
{ | |
MaxDegreeOfParallelism = 8, | |
CancellationToken = this.cancellationTokenSource.Token | |
}; | |
try | |
{ | |
Parallel.ForEach(list, po, (item, loopState) => | |
{ | |
if (loopState.ShouldExitCurrentIteration || loopState.IsExceptional) | |
loopState.Stop(); | |
po.CancellationToken.ThrowIfCancellationRequested(); | |
// do stuff here | |
}); | |
} | |
catch (OperationCanceledException) | |
{ | |
// we're aborting, tidy up | |
} | |
catch (Exception e) | |
{ | |
// report error here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note: "Do stuff" is wrapped in a try catch, so it will never throw. Can AggregateExceptions be thrown by another means?