title: What is correct way to combine long-running tasks with async / await pattern?
body: <p>I have a "High-Precision" timer class that I need to be able to be start, stop &amp; pause / resume.  To do this, I'm tying together a couple of different examples I found on the internet, but I'm not sure if I'm using Tasks with asnyc / await correctly.</p>
  
  <p>Here is my relevant code:</p>
  
  <pre><code>//based on http://haukcode.wordpress.com/2013/01/29/high-precision-timer-in-netc/
  public class HighPrecisionTimer : IDisposable
  {
      Task _task;
      CancellationTokenSource _cancelSource;
  
      //based on http://blogs.msdn.com/b/pfxteam/archive/2013/01/13/cooperatively-pausing-async-methods.aspx
      PauseTokenSource _pauseSource;
  
      Stopwatch _watch;
      Stopwatch Watch { get { return _watch ?? (_watch = Stopwatch.StartNew()); } }
  
      public bool IsPaused
      {
          get { return _pauseSource != null &amp;&amp; _pauseSource.IsPaused; }
          private set
          {
              if (value)
              {
                  _pauseSource = new PauseTokenSource();
              }
              else
              {
                  _pauseSource.IsPaused = false;
              }
          }
      }
  
      public bool IsRunning { get { return !IsPaused &amp;&amp; _task != null &amp;&amp; _task.Status == TaskStatus.Running; } }
  
      public void Start()
      {
          if (IsPaused)
          {
              IsPaused = false;
          }
          else if (!IsRunning)
          {
              _cancelSource = new CancellationTokenSource();
              _task = new Task(ExecuteAsync, _cancelSource.Token, TaskCreationOptions.LongRunning);
              _task.Start();
          }
      }
  
      public void Stop()
      {
          if (_cancelSource != null)
          {
              _cancelSource.Cancel();
          }
      }
  
      public void Pause()
      {
          if (!IsPaused)
          {
              if (_watch != null)
              {
                  _watch.Stop();
              }
          }
  
          IsPaused = !IsPaused;
      }
  
      async void ExecuteAsync()
      {
          while (!_cancelSource.IsCancellationRequested)
          {
              if (_pauseSource != null &amp;&amp; _pauseSource.IsPaused)
              {
                  await _pauseSource.Token.WaitWhilePausedAsync();
              }
  
              // DO CUSTOM TIMER STUFF...
          }
  
          if (_watch != null)
          {
              _watch.Stop();
              _watch = null;
          }
  
          _cancelSource = null;
          _pauseSource = null;
      }
  
      public void Dispose()
      {
          if (IsRunning)
          {
              _cancelSource.Cancel();
          }
      }
  }
  </code></pre>
  
  <p>Can anyone please take a look and provide me some pointers on whether I'm doing this correctly?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have tried modifying my code per Noseratio's comments below, but I still cannot figure out the syntax. Every attempt to pass the <em>ExecuteAsync()</em> method to either <strong>TaskFactory.StartNew</strong> or <strong>Task.Run</strong>, results in a compilation error like the following:</p>
  
  <p>"The call is ambiguous between the following methods or properties: TaskFactory.StartNew(Action, CancellationToken...) and TaskFactory.StartNew&lt;Task&gt;(Func&lt;Task&gt;, CancellationToken...)".</p>
  
  <p>Finally, is there a way to specify the LongRunning TaskCreationOption without having to provide a TaskScheduler?</p>
  
  <pre><code>async **Task** ExecuteAsync()
  {
      while (!_cancelSource.IsCancellationRequested)
      {
          if (_pauseSource != null &amp;&amp; _pauseSource.IsPaused)
          {
              await _pauseSource.Token.WaitWhilePausedAsync();
          }
          //...
      }
  }
  
  public void Start()
  {
      //_task = Task.Factory.StartNew(ExecuteAsync, _cancelSource.Token, TaskCreationOptions.LongRunning, null);
  
      //_task = Task.Factory.StartNew(ExecuteAsync, _cancelSource.Token);
  
      //_task = Task.Run(ExecuteAsync, _cancelSource.Token);
  
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I think I've narrowed this down, but still not sure about the correct syntax.  Would this be the right way to create the task so that the consumer / calling code continues on, with the task spinning-up and starting on a new asynchronous thread?</p>
  
  <pre><code>_task = Task.Run(async () =&gt; await ExecuteAsync, _cancelSource.Token);
  
  //**OR**
  
  _task = Task.Factory.StartNew(async () =&gt; await ExecuteAsync, _cancelSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
  </code></pre>