Namespace: System.Linq
Enum ParallelExecutionMode
Specifies the execution mode for parallel operations.
Inheritance
object
ValueType
Enum
ParallelExecutionMode
Members
-
Default
- The default execution mode. The runtime chooses the most appropriate execution mode based on the workload. -
ForceParallelism
- Forces the operation to execute in parallel, even if it might not be the most efficient choice.
Remarks
The ParallelExecutionMode
enumeration is used with the ParallelOptions
class to specify how a parallel query should be executed.
For example, you can use it to force parallel execution even for small data sets or when the runtime might otherwise choose sequential execution.
However, forcing parallelism can sometimes lead to performance degradation if not used judiciously.
Example
using System;
using System.Linq;
using System.Threading.Tasks;
public class Example
{
public static void Main(string[] args)
{
var numbers = Enumerable.Range(1, 10000);
var options = new ParallelOptions
{
MaxDegreeOfParallelism = 4,
ExecutionMode = ParallelExecutionMode.ForceParallelism
};
var sum = numbers.AsParallel()
.WithExecutionMode(options.ExecutionMode)
.WithDegreeOfParallelism(options.MaxDegreeOfParallelism)
.Sum();
Console.WriteLine($"Sum: {sum}");
}
}