title: Cancelling a task within a task
body: <p>I am trying to cancel a Task&lt;> by calling the CancellationTokenSource.Cancel() method within the task, but I cannot get it to work.</p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>TaskScheduler ts = TaskScheduler.Current;
  
  CancellationTokenSource cts = new CancellationTokenSource();
  
  Task t = new Task( () =&gt;
  {
      Console.WriteLine( "In Task" );
      cts.Cancel();
  }, cts.Token );
  
  Task c1 = t.ContinueWith( antecedent =&gt;
  {
      Console.WriteLine( "In ContinueWith 1" );
  }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, ts );
  
  Task c2 = c1.ContinueWith( antecedent =&gt;
  {
      Console.WriteLine( "In ContinueWith 2" );
  }, TaskContinuationOptions.NotOnCanceled );
  
  t.Start();
  
  Console.ReadKey();
  
  Environment.Exit( 1 );
  </code></pre>
  
  <p>This print outs:</p>
  
  <pre><code>In Task
  In ContinueWith 1
  In ContinueWith 2
  </code></pre>
  
  <p>What I expected was this:</p>
  
  <pre><code>In Task
  </code></pre>
  
  <p>Am I missing something here?  Can tasks only be cancelled <strong>outside</strong> of the task?</p>