title: Threading causing blocking; Not present when synchronous
body: <p>I am stumped.  I have a bit of code that goes out and searches my database.  It does 7 different searches at the same time. </p>
  
  <p>To get them to run at the same time I use threading.  I have tried several different ways to call it, but right now I am using something like this:</p>
  
  <pre><code>var encounterResults = new List&lt;IODataList&lt;ISearchResultItem&gt;&gt;();
  
  Parallel.ForEach(functions, function =&gt;
  {
      var results = function(searchParam, ClientId);
      encounterResults.Add(results);
  });
  </code></pre>
  
  <p>It all works very well when run by itself.</p>
  
  <p>However, when my user clicks my Save Button, I call a WCF service operation asynchronously.  While that service operation is being called, if the user fires of a search (using the above code), the search waits until the WCF Service operation completes, then the search completes.</p>
  
  <p>I cannot see anything that the search cares about that the save button also cares about.  (They don't share any objects.)</p>
  
  <p>Just for fun, I tried changing my search to look like this </p>
  
  <pre><code>foreach (var function in functions)
  {
      var results = function(searchParam, ClientId);
      encounterResults.Add(results);
  }
  </code></pre>
  
  <p>When I run it like that (synchronously) it has no problems with the save button.  (The search is not blocked.)</p>
  
  <p>But some of my searches can take a while, so I worry about just going synchronous.</p>
  
  <p>I have tried all I can think of to see what could be causing this block, but I am stumpped.  I am wondering if it is something related to threading itself.  (For example I thought I might be out of threads, but ThreadPool.GetAvailableThreads shows I am only using a few.)</p>
  
  <p>Anyone know what could be causing my threads to block?</p>
  
  <p><strong>UPDATE:</strong>  </p>
  
  <p>I tried having only one <code>function</code> in my collection of <code>functions</code> and the blocking did not occur when run though the threaded method.  However if I add just one more it blocks.  So something about two or more together causes this.</p>