using System.Threading.Tasks;
public static void Main()
int[] nums = Enumerable.Range(0, 100000).ToArray();
CancellationTokenSource cts = new CancellationTokenSource();
ParallelOptions po = new ParallelOptions();
po.CancellationToken = cts.Token;
po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
Console.WriteLine($"ProcessorCount: {System.Environment.ProcessorCount}");
Task.Factory.StartNew(async () =>
Console.WriteLine($"Task started {DateTime.Now}");
Console.WriteLine($"Task ended {DateTime.Now}");
Console.WriteLine($"Parallel started {DateTime.Now}");
Parallel.ForEach(nums, po, (num) =>
double d = Math.Sqrt(num);
Console.WriteLine($"Sqrt of {num} is {d} on {Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine($"Parallel ended {DateTime.Now}");
catch (OperationCanceledException e)
Console.WriteLine(e.Message);