title: Does PLINQ respect SynchronizationContext?
body: <p>Say, I have the following code:</p>
  
  <pre><code>IPrincipal capturedPrincipal = Thread.CurrentPrincipal;
  myseq.AsParallel().Select(x =&gt;
  {
      Thread.CurrenctPrincipal = capturedPrincipal;
      /*call code protected with CAS*/
  });
  </code></pre>
  
  <p>to be sure <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.currentprincipal.aspx" rel="nofollow noreferrer"><code>Thread.CurrenctPrincipal</code></a> will be propagated to every thread where <code>Select</code>'s delegate will be executed on. I've realized that if I had proper <a href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx" rel="nofollow noreferrer"><code>SynchronizationContext</code></a> set up this would happen automatically. So does PLINQ use <code>SynchronizationContext</code> when queuing work items on <code>ThreadPool</code>? And if no, why?</p>
  
  <p>p.s.</p>
  
  <p>I think it's important to note that the code above is executed in WCF environment hosted under IIS/WAS (no ASP.NET compatibility).</p>
  
  <p><strong>Edit:</strong>
  Found <a href="https://stackoverflow.com/questions/8319439/does-the-thread-identity-get-transferred-when-using-plinq-extensions">a similar question</a> confirming same behavior I witness.</p>
  
  <p><strong>Edit2:</strong>
  Modified casperOne's test a bit and it fails saying threadid <em>is the same</em>:</p>
  
  <pre><code>    [Test]
      public void Test1()
      {
          var principal = new GenericPrincipal(new GenericIdentity("test"), new string[0]);
          Thread.CurrentPrincipal = principal;
          int threadID = Thread.CurrentThread.ManagedThreadId;
  
          Enumerable.Range(0, 4000).AsParallel()
              .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
              .Select(x =&gt;
                  {
                      Assert.AreSame(Thread.CurrentPrincipal, principal);
                      Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, threadID);
                      return x;
                  })
              .ToArray();
      }
  </code></pre>