title: How will Parallel.Foreach behave when Iterating over the results of a method call?
body: <p><strong>Scope:</strong></p>
  
  <p>I am currently implementing an application that uses <a href="http://aws.amazon.com/sqs/" rel="nofollow">Amazon SQS Service</a> as a provider of data for this program to process.</p>
  
  <p>Since I need a parallel processing over the messages dequeued from this queue, this is what I've did.</p>
  
  <pre><code>Parallel.ForEach (GetMessages (msgAttributes), new ParallelOptions { MaxDegreeOfParallelism = threadCount }, message =&gt;
          {
               // Processing Logic
          });
  </code></pre>
  
  <p>Here's the header of the "GetMessages" method:</p>
  
  <pre><code>    private static IEnumerable&lt;Message&gt; GetMessages (List&lt;String&gt; messageAttributes = null)
      {
          // Dequeueing Logic... 10 At a Time
  
          // Yielding the messages to the Parallel Loop
          foreach (Message awsMessage in messages)
          {
             yield return awsMessage;
          }
      }
  </code></pre>
  
  <p><strong>How will this work ?:</strong></p>
  
  <p>My initial thought about how this would work was that the <code>GetMessages</code>method would be executed whenever the thread's had no work (or a good number of threads had no work, something like an internal heuristic to measure this). That being said, to me, the <code>GetMessages</code> method would than, distribute the messages to the <code>Parallel.For</code> working threads, which would process the messages and wait for the <code>Parallel.For handler</code> to give them more messages to work.</p>
  
  <p><strong>Problem? I was wrong...</strong></p>
  
  <p>The thing is that, I was wrong. Still, I have no idea on what's happening in this situation. </p>
  
  <p>The number of messages being dequeued is way too high, and it grews by powers of 2 every time they get dequeued. The dequeueing count (messsages) goes as following:</p>
  
  <ol>
  <li>Dequeue is Called: Returns 80 Messages</li>
  <li>Dequeue is Called: Returns 160 Messages</li>
  <li>Dequeue is Called: Returns 320 Messages (and so forth)</li>
  </ol>
  
  <p>After a certain point, the number of messages being dequeued, or, in this case, waiting to be processed by my application is too high and I end up running out of memory.</p>
  
  <p><strong>More Information:</strong></p>
  
  <p>I am using thread-safe <code>InterLocked</code> operations to increment counters mentioned above.</p>
  
  <p>The number of threads being used is 25 (for the <code>Parallel.Foreach</code>)</p>
  
  <p>Each "GetMessages" will return up to 10 messages (as an IEnumerable, yielded).</p>
  
  <p><strong>Question: What exactly is happening on this scenario ?</strong></p>
  
  <p>I am having a hard-time trying to figure out what exactly is going on. Is my <code>GetMessages</code> method being invoked by each thread once it finishes the "Processing Loop", hence, leading to more and more messages being dequeued ?</p>
  
  <p>Is the call to the "GetMessages", made by a single thread, or is it being called by multiple threads ? </p>