title: How to ensure that Task.Factory.StartNew doesn't slow down the main thread?
body: <p>There's a bunch of compressed data chunks that has to be asynchronously <em>deflated</em>  - <strong>without blocking or slowing down the main thread</strong> in any shape or form.</p>
  
  <p>Decompressed chunks will be used by the main thread as soon as they are decompressed.</p>
  
  <p>Currently I do it like this:</p>
  
  <pre><code>foreach (var chunkPair in compressedChunkData)
  {                        
      var task = Task.Factory.StartNew&lt;Chunk&gt;(() =&gt;
      {
          var compressedBytes = Convert.FromBase64String(chunkPair.Value);
          var chunk = Decompress(compressedBytes);
          return chunk;
      }).ContinueWith((finishedTask) =&gt;
      {
          var chunk = finishedTask.Result;
          TaskFinishActions.Enqueue(() =&gt;
          {
              chunk.PostSerialize();
              document.Chunks.Add(chunkPair.Key, chunk);
          });
      });
  }
  // By the time we get here 20ms has passed!!!
  </code></pre>
  
  <p>The problem is that this seems to hijack the core the mainthread is running on, this butchers the performance.</p>
  
  <p>Is there a way to make <code>TaskFactory</code> have thread per core and context switch away from mainthread only in those brief moments when mainthread is blocked?</p>
  
  <p><strong>EDIT</strong>: the foreach loop is not the only part of the code which becomes slow, as long as there sizable amount of decompression tasks running, <em>mainthread slows down significantly</em>.</p>
  
  <p><strong>EDIT2</strong>: New data to decompress arrive all the time, the loop is not ran only once:</p>
  
  <ul>
  <li>Lets say you have 250 items arriving in <code>compressedChunkData</code> first </li>
  <li>Next frame you have 10 items, next 12, next 0, next 2, etc.</li>
  </ul>