How to specify the number of parallel tasks executed in Parallel.ForEach? <p>I have ~500 tasks, each of them takes ~5 seconds where most of the time is wasted on waiting for the remote resource to reply. I would like to define the number of threads that should be spawned myself (after some testing) and run the tasks on those threads. When one task finishes I would like to spawn another task on the thread that became available.</p> <p>I found <code>System.Threading.Tasks</code> the easiest to achieve what I want, but I think it is impossible to specify the number of tasks that should be executed in parallel. For my machine it's always around 8 (quad core cpu). Is it possible to somehow tell how many tasks should be executed in parallel? If not what would be the easiest way to achieve what I want? (I tried with threads, but the code is much more complex). I tried increasing <code>MaxDegreeOfParallelism</code> parameter, but it only limits the maximum number, so no luck here...</p> <p>This is the code that I have currently:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { private static List&lt;string&gt; _list = new List&lt;string&gt;(); private static int _toProcess = 0; static void Main(string[] args) { for (int i = 0; i &lt; 1000; ++i) { _list.Add("parameter" + i); } var w = new Worker(); var w2 = new StringAnalyzer(); Parallel.ForEach(_list, new ParallelOptions() { MaxDegreeOfParallelism = 32 }, item =&gt; { ++_toProcess; string data = w.DoWork(item); w2.AnalyzeProcessedString(data); }); Console.WriteLine("Finished"); Console.ReadKey(); } static void Done(Task&lt;string&gt; t) { Console.WriteLine(t.Result); --_toProcess; } } class Worker { public string DoWork(string par) { // It's a long running but not CPU heavy task (downloading stuff from the internet) System.Threading.Thread.Sleep(5000); return par + " processed"; } } class StringAnalyzer { public void AnalyzeProcessedString(string data) { // Rather short, not CPU heavy System.Threading.Thread.Sleep(1000); Console.WriteLine(data + " and analyzed"); } } } </code></pre>