title: Execute task on current thread
body: <p>Is it possible to force a task to execute synchronously, on the current thread?</p>
  
  <p>That is, is it possible, by e.g. passing some parameter to <code>StartNew()</code>, to make this code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>Task.Factory.StartNew(() =&gt; ThisShouldBeExecutedSynchronously());
  </code></pre>
  
  <p>behave like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>ThisShouldBeExecutedSynchronously();
  </code></pre>
  
  <p><strong>Background:</strong></p>
  
  <p>I have an interface called <code>IThreads</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public interface IThreads
  {
      Task&lt;TRet&gt; StartNew&lt;TRet&gt;(Func&lt;TRet&gt; func);
  }
  </code></pre>
  
  <p>I would like to have two implemenetations of this, one normal that uses threads:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Threads : IThreads
  {
      public Task&lt;TRet&gt; StartNew&lt;TRet&gt;(Func&lt;TRet&gt; func)
      {
          return Task.Factory.StartNew(func);
      }
  }
  </code></pre>
  
  <p>And one that does not use threads (used in some testing scenarios):</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class NoThreading : IThreads
  {
      public Task&lt;TRet&gt; StartNew&lt;TRet&gt;(Func&lt;TRet&gt; func)
      {
          // What do I write here?
      }
  }
  </code></pre>
  
  <p>I could let the <code>NoThreading</code> version just call <code>func()</code>, but I want to return an instance of <code>Task&lt;TRet&gt;</code> on which I can perform operations such as <code>ContinueWith()</code>.</p>