如何限制 Parallel.ForEach? - How can I limit Parallel.ForEach?

語言: CN / TW / HK

問題:

I have a Parallel.ForEach() async loop with which I download some webpages.我有一個 Parallel.ForEach() 非同步迴圈,我用它下載了一些網頁。 My bandwidth is limited so I can download only x pages per time but Parallel.ForEach executes whole list of desired webpages.我的頻寬有限,所以我每次只能下載 x 頁,但 Parallel.ForEach 執行所需網頁的整個列表。

Is there a way to limit thread number or any other limiter while running Parallel.ForEach?有沒有辦法在執行 Parallel.ForEach 時限制執行緒數或任何其他限制器?

Demo code:演示程式碼:

Parallel.ForEach(listOfWebpages, webpage => {
  Download(webpage);
});

The real task has nothing to do with webpages, so creative web crawling solutions won't help.真正的任務與網頁無關,因此創造性的網路抓取解決方案無濟於事。


解決方案:

參考一: https://stackoom.com/question/cysk
參考二: How can I limit Parallel.ForEach?
「其他文章」