Overview

Cancellation is achieved by:

  1. Supplying a CancellationToken to the Parallel.For or Parallel.ForEach method’s ParallelOptions parameter
  2. Enclosing the parallel call in a try-catch block

Example

int[] nums = Enumerable.Range(0, 1_000_000).ToArray();

using CancellationTokenSource cts = new();

ParallelOptions po = new();
po.CancellationToken = cts.Token;

try
{
    Parallel.ForEach(nums, po, (num) =>
    {
    double d = Math.Sqrt(num);
    Console.WriteLine($"{d} on {Thread.CurrentThread.ManagedThreadId}");
    });
}
catch
{
    
}