I Honestly don't understand when to use WithDegreeOfParallelism and how it can improve performance?
Not sure in what context limiting the number of maximum number of tasks that are executing the query
I Honestly don't understand when to use WithDegreeOfParallelism and how it can improve performance?
Not sure in what context limiting the number of maximum number of tasks that are executing the query
Second link when searching for it gives the Introduction to PLINQ page, which gives two examples:
This is useful when you want to make sure that other processes running on the computer receive a certain amount of CPU time.
In cases where a query is performing a significant amount of non-compute-bound work such as File I/O, it might be beneficial to specify a degree of parallelism greater than the number of cores on the machine.
If neither of these are compelling to you, or match how you're using PLINQ, and you can't think of any other cases (e.g. if they're going to be competing to use some other resource which has a finite number of instances available) then I'd suggest leaving it out.
You can use this method in parallel loops. For example I used paralellizm in process of records in DataGridView.
int tasks = Environment.ProcessorCount;
DataGrid.Rows.AsParallel().OfType<DataGridViewRow>().WithDegreeOfParallelism(tasks).ForAll(i =>
{
// some code
});
By default DegreeOfParallelism is equal to number of processor cores. This parameter defines how many concurrent tasks (i.e. threads) will be created at processing. When I use default degree (4 cores) my application "hangs" a little so I decided to decrease degree of parallelism to 2. In any case you should select the value of this parameter as you want (or give user the option to force him make a selection ;) )