eugeneK eugeneK - 1 year ago 151
C# Question

How can I limit Parallel.ForEach?

I have a Parallel.ForEach() async loop with which I download some webpages. My bandwidth is limited so I can download only x pages per time but Parallel.ForEach executes whole list of desired webpages.

Is there a way to limit thread number or any other limiter while running Parallel.ForEach?

Demo code:

Parallel.ForEach(listOfWebpages, webpage => {

The real task has nothing to do with webpages, so creative web crawling solutions won't help.

Answer Source

You can specify a MaxDegreeOfParallelism in a ParallelOptions parameter:

    new ParallelOptions { MaxDegreeOfParallelism = 4 },
    webpage => { Download(webpage); }

MSDN: Parallel.ForEach

MSDN: ParallelOptions.MaxDegreeOfParallelism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download