title: How to guarantee that a Task runs synchronously on the current thread?
body: <p>I'm aware that there are several similar questions on this and other websites, but the standard way of doing this doesn't appear to work in my situation for some reason. The <em>normal</em> way to accomplish this requirement is to use <code>TaskScheduler.FromCurrentSynchronizationContext()</code> as the <code>TaskScheduler</code> input parameter in the relevant <a href="https://msdn.microsoft.com/en-us/library/dd781658(v=vs.110).aspx" rel="nofollow"><code>Task.Factory.StartNew</code> overload</a>:</p>
  
  <pre><code>// Set uiTaskScheduler whilst on the UI thread
  TaskScheduler uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
  ...
  Task.Factory.StartNew(() =&gt; SomeMethodToRunAsynchronously(), 
      CancellationToken.None, TaskCreationOptions.None, uiTaskScheduler);
  </code></pre>
  
  <p>It appears that this is enough to schedule a <code>Task</code> to run on the UI thread, but it doesn't seem to work in my situation. In my case, I have a <code>UiThreadManager</code> class which among other things has a <code>RunAsynchronously</code> method in it:</p>
  
  <pre><code>public Task RunAsynchronously(Action method)
  {
      return Task.Run(method);
  }
  </code></pre>
  
  <p>This part works just fine. The problem that I face is that when running unit tests, this class is replaced with a <code>MockUiThreadManager</code> class (both implement an <code>IUiThreadManager</code> interface which is used by the application code) and I can't seem to force <em>this</em> method to run on the UI thread:</p>
  
  <pre><code>public Task RunAsynchronously(Action method)
  {
      return Task.Factory.StartNew(() =&gt; method(), 
          CancellationToken.None, TaskCreationOptions.None, UiTaskScheduler);
  }
  </code></pre>
  
  <p>The <code>MockUiThreadManager</code> class has a <code>static UiTaskScheduler</code> property which is set (like below) on the UI thread, so I assumed that all code passing through the above method would run as expected on that thread:</p>
  
  <pre><code>SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
  MockUiThreadManager.UiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
  </code></pre>
  
  <p>However, when running a unit test, I noticed that it finished <em>before</em> the code that it was testing. I therefore added some breakpoints and called <code>System.Threading.Thread.CurrentThread.ManagedThreadId</code> in the Visual Studio Immediate Window at each breakpoint and sure enough, when the code passed through the above method, the thread ID changed.</p>
  
  <p>So basically, I'm looking for a way to <em>fake</em> a <code>Task</code> based asynchronous call for the <code>RunAsynchronously</code> method that is run during unit tests that will ensure that the method actually runs synchronously on the UI thread. Does anybody see what I have done wrong, or have any other suggestions?</p>
  
  <p>UPDATE >>></p>
  
  <p>Ok, I've used the wrong terminology here regarding the UI thread. Unit tests don't run on the UI thread because there's no UI... however, the situation remains the same. To clarify, I just need my tests to run the application code synchronously and on the <em>main</em> single thread that it starts on. The problem is that when the application is running, there is a lot of <code>Task</code> based asynchronous code and this needs to be tested via the <code>MockUiThreadManager</code> class synchronously.</p>