I have a simple question, i have following simple Parallel for loop. this for loop is part of windows service. I want to stop the loop, when someone stops the service. I can find three ways to stop parallel for, which is in if condition. What is the best way of stopping the parallel for loop and what are the differences?
CancellationTokenSource cancellationToken = new CancellationTokenSource();
ParallelOptions options = new ParallelOptions();
options.CancellationToken = cancellationToken.Token;
Parallel.For(0, maximum_operations, options, (a, loopState) =>
{
{
//Do something
if(!KeepProcessing)
{
//loopState.Break();
//loopState.Stop();
cancellationToken.Cancel();
}
}
});