Parallel Options
Options that configure the operation of methods on the Parallel class.
MaxDegreeOfParallelism
Gets or sets the maximum number of concurrent tasks enabled by this ParallelOptions instance.
ParallelOptions options= new ParallelOptions();
options.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
Parallel.For(
0,
9,
options,
(i) =>
{
Console.WriteLine("Thread={0}, i={1}", Thread.CurrentThread.ManagedThreadId, i);
}
);
System.Environment.ProcessorCount
- Gets the number of processors available to the current process.
CancellationToken
Gets or sets the CancellationToken associated with this ParallelOptions instance.
CancellationTokenSource cts = new CancellationTokenSource();
ParallelOptions po = new ParallelOptions();
po.CancellationToken = cts.Token;