DMac the Destroyer DMac the Destroyer - 16 days ago 7
ASP.NET (C#) Question

Parallel.ForEach Set MaxDegreeOfParallelism globally

Say I have a situation where I really want to use

Parallel.ForEach
instead of a regular
foreach
loop because it's much more performant (and way cooler), but having
.NET
determine the degree of parallelism for me leads to disastrous effects.

Now say that I've discovered the magic number that I need to place in the
ParallelOptions.MaxDegreeOfParallelism
parameter so that my application is super tasty fast and not super tasty broken.

Instead of having to remember to include a
ParallelOptions
parameter with the magic number every time I invoke
Parallel.ForEach
(or any other derivative of it), is there any way to specify the degree of parallelism at the
web.config
level? Or some other approach where the value can be set globally and invisibly so I don't have to rely on the memory of myself and fellow developers any time we want to gain the benefits of parallelism?

Answer

The short answer is No, you can't configure this parameter using a config file, however you can still have a const variable that all your call can refer to.