using System.Threading.Tasks;
var cts = new CancellationTokenSource(700);
cts.Token.Register(() => Print($"The Token was canceled."));
var options = new ParallelOptions()
MaxDegreeOfParallelism = 2,
CancellationToken = cts.Token
Print("Before starting the Parallel.Invoke.");
Parallel.Invoke(options, Enumerable.Range(1, 20).Select(i => new Action(() =>
Print($"Running action #{i}");
catch (OperationCanceledException)
Print("The Parallel.Invoke was canceled.");
static void Print(object value)
Console.WriteLine($@"{DateTime.Now:HH:mm:ss.fff} [{Thread.CurrentThread
.ManagedThreadId}] > {value}");