47
1
using System;
2
using System.Linq;
3
using System.Threading;
4
using System.Threading.Tasks;
5
6
public class Program
7
{
8
public static void Main()
9
{
10
int[] nums = Enumerable.Range(0, 100000).ToArray();
11
CancellationTokenSource cts = new CancellationTokenSource();
12
13
// Use ParallelOptions instance to store the CancellationToken
14
ParallelOptions po = new ParallelOptions();
15
po.CancellationToken = cts.Token;
16
po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
17
Console.WriteLine($"ProcessorCount: {System.Environment.ProcessorCount}");
18
19
// Run a task so that we can cancel from another thread.
20
Task.Factory.StartNew(async () =>
21
{
22
Console.WriteLine($"Task started {DateTime.Now}");
23
await Task.Delay(10);
24
cts.Cancel();
Cached Result