CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); Task.Factory.StartNew(() =>
{
Thread.Sleep();
cancellationTokenSource.Cancel();
Console.WriteLine("Token cancelled");
}); ParallelOptions parallelLoopOptions =
new ParallelOptions()
{
CancellationToken = cancellationTokenSource.Token
}; try
{
Parallel.For(, Int64.MaxValue, parallelLoopOptions, index =>
{
double result = Math.Pow(index, );
Console.WriteLine("Index {0}, result {1}", index, result);
Thread.Sleep();
});
}
catch (OperationCanceledException)
{
Console.WriteLine("Cancellation exception caught!");
}