title: Parallel.ForEach error: The query processor could not start the necessary thread resources for parallel query execution
body: <p>I am having an issue when executing multiple threads using TPL Parallel.ForEach() method in c#. The multi-processing accesses the database, and I also wrap the each action with a using statements so that it disposes the connection after each execution.</p>
  
  <p>The error I get says: <strong>The query processor could not start the necessary thread resources for parallel query execution</strong></p>
  
  <p>After a lot of googling, SQL is out of resources due to it being too busy and or causing some memory leaks.</p>
  
  <p>I have also tried setting the ParallelOptions parameter in the Parallel.ForEach() and setting the <em>MaxDegreeOfParallelism = 2</em> but this does not help. i.e.</p>
  
  <pre><code>Parallel.ForEach(customerNumbers, cno =&gt;
  {
     using (var ctx = new MyContext())
     {
         // do database call
     }
  }, new ParallelOptions { MaxDegreeOfParallelism = 2 });
  </code></pre>
  
  <p>Can anyone provide me with a light on how to fix this?</p>