var cts new CancellationTokenSou rce var AsParallel WithCancellation c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
var cts = new CancellationTokenSource();
var q = a.AsParallel().WithCancellation(cts.Token).Where(x=>Filter(x)).Select(x=>DoWork(x);
-- separate thread --
foreach (var e in q) { } // Statement 1
-- separate thread --
var l = q.ToList(); // Statement 2
-- separate thread --
cts.Cancel(); // this will attempt to cancel any in-flight queries,
// including both statements 1 and 2