title: Parallel.ForEach - Where is it running on single core machines?
body: <p>I understand that the new <a href="https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl" rel="nofollow noreferrer">TPL</a> (Task Parallel Library) has implemented the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreach" rel="nofollow noreferrer"><code>Parallel.ForEach</code></a> such that it works with &quot;expressed parallelism.&quot;  This means it does not guarantee that your delegates will run in multiple threads, but rather it checks to see if the host platform has multiple cores, and if true, only then does it distribute the work across the cores (essentially 1 thread per core).</p>
  <p>If the host system does not have multiple cores (getting harder and harder to find such a computer) then it will run your code sequentially like a &quot;regular&quot; for each loop would.  Pretty cool stuff, frankly.</p>
  <p>Normally I would do something like the following to place my long-running operation on a background thread from the <code>ThreadPool</code>:</p>
  <pre><code>ThreadPool.QueueUserWorkItem(new WaitCallback(targetMethod), new Object2PassIn() );
  </code></pre>
  <p>In a situation whereby the host computer only has a single core does the TPL's <code>Parallel.ForEach</code> automatically places the invocation on a background thread?  Or, should I manually invoke any TPL calls from a background thread so that if I am executing from a single core computer at least that logic will be off of the GUI's dispatching thread?</p>
  <p>My concern is if I leave the TPL in charge of all this I want to ensure if it determines it's a single core box that it still marshals the code that's inside of the <code>Parallel.ForEach</code> loop onto a background thread like I would have done, to not block my GUI.</p>