title: Is Task.Factory.StartNew() guaranteed to use another thread than the calling thread?
body: <p>I am starting a new task from a function but I would not want it to run on the same thread. I don't care which thread it runs on as long as it is a different one (so the information given in <a href="https://stackoverflow.com/questions/9410930/is-task-factory-startnew-guaranteed-to-create-at-least-one-new-thread">this question</a> does not help).</p>
  
  <p>Am I guaranteed that the below code will always exit <code>TestLock</code> before allowing <code>Task t</code> to enter it again? If not, what is the recommended design pattern to prevent re-entrency?</p>
  
  <pre><code>object TestLock = new object();
  
  public void Test(bool stop = false) {
      Task t;
      lock (this.TestLock) {
          if (stop) return;
          t = Task.Factory.StartNew(() =&gt; { this.Test(stop: true); });
      }
      t.Wait();
  }
  </code></pre>
  
  <p><strong>Edit:</strong> Based on the below answer by Jon Skeet and Stephen Toub, a simple way to deterministically prevent reentrancy would be to pass a CancellationToken, as illustrated in this extension method:</p>
  
  <pre><code>public static Task StartNewOnDifferentThread(this TaskFactory taskFactory, Action action) 
   {
      return taskFactory.StartNew(action: action, cancellationToken: new CancellationToken());
  }
  </code></pre>