title: How to run method synchronously from test project (no await)
body: <p>I'm pretty new in using Task and the whole concept around it. Also I'm using <strong>Net 4.0</strong> (<em>VSTO development for Office 2007</em>)</p>
  
  <p>I'd like to understand one thing I wrote in my app that is working in production but writing unit test for it ended up with using <code>Thread.Sleep</code> and checking if the task run to completion or not</p>
  
  <h2>Background</h2>
  
  <p>The production code is pretty simple, it calls a service to check out an environment status (<em>PROD, STAGE, QA etc.</em>) it's done in a (<code>void</code>) method via <code>Task</code>, inside the Task body it sets a property that is updating UI (WPF) and it's working OK. note I'm not using <strong><code>Wait</code></strong> in that method. If the Task fails I continue with another one with the <strong><code>OnlyOnFaulted</code></strong></p>
  
  <h2>Problem</h2>
  
  <p>I try to unit test the method (void) so I'd like to run the method running <strong>synchronously</strong> and I'm failing there.
  I read about <a href="https://msdn.microsoft.com/cs-cz/library/dd321435%28v=vs.110%29.aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow noreferrer">RunSynchronously</a>, also saw people using  task.<strong><code>Result</code></strong> (<em>which doesn't seems to be available for 4.0</em>), also tried <code>Scheduler.Current</code>, <code>AttachedToParent</code> etc. </p>
  
  <p>I think the problem is my design, I guess I'm trying to call a method and saying "Hey, run synchronously" and it does but inside the method is another task that run async so no matter what it will always run async. Am I right? Is there a way how to test it without a loop in my test project and waiting for <code>RunToCompletion</code>?</p>
  
  <p>Here is sample code I created for better understanding, I simplified it a bit. In real implementation there is a proper Test project in my solution, I'm mocking the service so it doesn't call the real one etc.</p>
  
  <pre><code>namespace ConsoleApplication1
  {
  public class Program
  {
      static void Main(string[] args)
      {
          ImagineThisIsInTestProject();
          Console.ReadLine();
  
          Console.WriteLine("Started");
          var cls = new Program();
          cls.MyProdMethod();
          Console.WriteLine("Finished");
          Console.ReadLine();
      }
  
      public string Status { get; private set; }
  
      public void MyProdMethod()
      {
          // I have more logic in my real method, serialization, handling errors
          Console.WriteLine("In my method");
          var task = Task.Factory.StartNew(() =&gt; {
                          Thread.Sleep(2000);
                          Console.WriteLine("Setting status");
                          Status = "Yes I set it";
                          }, CancellationToken.None);
          Console.WriteLine("Finishing my method");
      }
  
      public static void ImagineThisIsInTestProject()
      {
          Console.WriteLine("TEST started");
          var sut = new Program();
          var task = new Task(() =&gt; sut.MyProdMethod());
          task.RunSynchronously(); // I would assume this will wait until the task is finished?
  
          // here my assert if status is set
          Console.WriteLine($"Status is - {sut.Status}");
          Console.WriteLine("TEST finished");
      }
  }
  }
  </code></pre>
  
  <p>Output of the above is</p>
  
  <pre><code>TEST started
  In my method
  Finishing my method
  Status is -
  TEST finished
  Setting status
  
  Started
  In my method
  Finishing my method
  Finished
  Setting status
  </code></pre>