title: C# 5 Async Await .Task.Factory.StartNew cancellation
body: <p>I have the async code that implements cancellation token. It's working but Im not pretty sure if this is the right way to do it so I just want feedback about it.</p>
  
  <p>Here is the actual code:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// 
      /// &lt;/summary&gt;
      private async void SaveData() {
  
          if (GetActiveServiceRequest() != null)
          {
              var tokenSource = new System.Threading.CancellationTokenSource();
  
  
              this.ShowWizardPleaseWait("Saving data...");
  
              var someTask = System.Threading.Tasks.Task&lt;bool&gt;.Factory.StartNew(() =&gt;
              {
  
                  bool returnVal = false;
  
                  // Set sleep of 7 seconds to test the 5 seconds timeout.
                  System.Threading.Thread.Sleep(7000);
  
                  if (!tokenSource.IsCancellationRequested)
                  {
                      // if not cancelled then save data
  
                      App.Data.EmployeeWCF ws = new App.Data.EmployeeWCF ();
                      returnVal = ws.UpdateData(_employee.Data);
                      ws.Dispose();
                  }
  
                  return returnVal;
  
              }, tokenSource.Token);
  
  
              if (await System.Threading.Tasks.Task.WhenAny(someTask, System.Threading.Tasks.Task.Delay(5000)) == someTask)
              {
                  // Completed
                  this.HideWizardPleaseWait();
                  if (someTask.Result)
                  {
                      this.DialogResult = System.Windows.Forms.DialogResult.OK;
                  }
                  else
                  {
                      this.DialogResult = System.Windows.Forms.DialogResult.Abort;
                  }
                  btnOK.Enabled = true;
                  this.Close();
              }
              else
              {
                  tokenSource.Cancel();
  
                  // Timeout logic
                  this.HideWizardPleaseWait();
                  MessageBox.Show("Timeout. Please try again.")
              }
  
  
          }
      }
  </code></pre>
  
  <p>Does async / await / cancellation code is well implemented?</p>
  
  <p>Thanks and appreciate the feedback.</p>