title: Parallel.For freezes after around 1370 iterations, no idea why
body: <p>I'm running a Parallel.For loop on a little over 7500 objects. Inside that for loop, I'm doing a number of things to each of those objects, specifically calling two web services and two internal methods. The web services simply inspect the object, process and return a string that i then set as a property on the object. Same goes for the two internal methods.</p>
  
  <p>I'm not writing anything out to disk or reading from disk.</p>
  
  <p>I also update the UI in a winforms app with a label and progress bar to let the user know where it's at. Here's the code:</p>
  
  <pre><code>var task = Task.Factory.StartNew(() =&gt;
  {
    Parallel.For(0, upperLimit, (i, loopState) =&gt;
    {
       if (cancellationToken.IsCancellationRequested)
          loopState.Stop();
       lblProgressBar.Invoke(
         (Action)
         (() =&gt; lblProgressBar.Text = string.Format("Processing record {0} of {1}.", (progressCounter++), upperLimit)));
       progByStep.Invoke(
         (Action)
         (() =&gt; progByStep.Value = (progressCounter - 1)));
  
        CallSvc1(entity[i]);
        Conversion1(entity[i]);
        CallSvc2(entity[i]);
        Conversion2(entity[i]);
    });
  }, cancellationToken);
  </code></pre>
  
  <p>This is taking place on a Win7 32bit machine.</p>
  
  <p>Any ideas as to why this suddenly freezes when the incrementer is around 1370 or so (It's been 1361, 1365 and 1371)?</p>
  
  <p>Any ideas as to how I can debug this and see what's locking if anything?</p>
  
  <p><strong>EDIT:</strong><br>
  Some answers to the comments below:<br>
  @BrokenGlass - No, no interop. I'll try the x86 compilation and let you know.</p>
  
  <p>@chibacity - Because it's on a background task, it's not freezing the UI. Up until the time it freezes, the progress bar and label tick along at about 2 every second. When it freezes, it just stops moving. I can verify that the number that it stops at has been processed, but no more. CPU usage on a dual core 2.2GHz is minimal during the operation at 3-4% each and 1-2% once it freezes.</p>
  
  <p>@Henk Holterman - It takes around 10-12 minutes to get to 1360 and yes, I can verify that all of those records have been processed but not the remaining records.</p>
  
  <p>@CodeInChaos - Thanks, I'll try that! The code does work if I take out the parallel, it just takes forever and a day. I haven't tried restricting the number of threads, but will.</p>
  
  <p><strong>EDIT 2:</strong><br>
  Some details as to what's going on with the webservices</p>
  
  <p>Basically what's going on with the web services is that they pass in some data and receive data (an XmlNode). That node is then used in the Conversion1 process which in turn sets another property on the entity which is sent to the CallSvc2 method and so on. It looks like this:</p>
  
  <pre><code>private void CallSvc1(Entity entity)
  {
      var svc = new MyWebService();
      var node = svc.CallMethod(entity.SomeProperty);
      entity.FieldToUpdate1.LoadXml(node.InnerXml);
  }
  private void Conversion1(Entity entity)
  {
      // Do some xml inspection/conversion stuff
      if (entity.FieldToUpdate1.SelectSingleNode("SomeNode") == "something") {
          entity.FieldToUpdate2 = SomethingThatWasConverted;
      }
      else {
          // Do some more logic
      }
  }
  private void CallSvc2(Entity entity)
  {
      var svc = new SomeOtherWebService();
      var xmlNode = svc.MethodToCall(entity.FieldToUpdate2.InnerXml);
      entity.AnotherXmlDocument.LoadXml(xmlNode.InnerXml);
  }
  </code></pre>
  
  <p>As you can see, it's pretty straightforward stuff. There's a lot going on in some of the conversion methods, but none of it should be blocking. And as noted below, there are 1024 threads in "waiting" status that are all sitting on the webservice calls. I read here <a href="http://www.albahari.com/threading/" rel="noreferrer">http://www.albahari.com/threading/</a> that the MaxThreads is defaulted to 1023 for .Net 4 on 32 bit machine. </p>
  
  <p>How can I release those waiting threads given what I have here?</p>