title: Using a CancellationToken for a Task.Run
body: <p>I have some code that uses <code>Task.Run</code> with a cancellation token.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class TaskObject
  {
      CancellationTokenSource _source = new CancellationTokenSource();
      public async Task TaskAction()
      {
          var task = Task.Run(async delegate
          {
              await TaskRun();
          }, _source.Token);
  
          //TaskCancel();
  
          try
          {
              task.Wait();
          }
          catch (Exception ex)
          {
  
          }
      }
      public async Task TaskRun()
      {
          if (_source.IsCancellationRequested)
          {
              _source.Token.ThrowIfCancellationRequested();
          }
  
          SpeechSynthesizer _speechSynthesizer = new SpeechSynthesizer();
          _speechSynthesizer.SpeakAsync("This is a test prompt");
      }
      public void TaskCancel()
      {
          _source.Cancel();
      }
  }
  </code></pre>
  
  <p>If I call the <code>TaskCancel()</code>in the <code>TaskAction()</code>, the task cancelled exception is caught.</p>
  
  <p>If I call the <code>TaskCancel()</code> from outside the object, the cancelled exception is not caught.</p>
  
  <p>Here is some code to demonstrate where the cancelled exception is not caught:</p>
  
  <pre><code>taskObject = new TaskObject();
  await taskObject.TaskAction();
  taskObject.TaskCancel();
  </code></pre>
  
  <p>How can I call the <code>TaskCancel()</code> from outside the object so that the cancelled exception is caught?</p>