title: Cancel an executing Task in a task queue
body: <p>I have a list of items which I need to process sequentially (but on a separate worker thread so to maintain UI responsiveness).  The important thing to note is that these items can run for a long time (5 - 10 seconds).</p>
  
  <pre><code>Task&lt;bool&gt; currentTask = null;
  
  foreach (var item in items)
  {
      var currentItem = item;
  
      // Add a new task to the sequential task queue
      if (currentTask == null)
          currentTask = Task.Factory.StartNew&lt;bool&gt;(() =&gt; 
          { 
              return currentItem.ProcessItem(); 
          }, processCancelTokenSource.Token);
      else
          currentTask = currentTask.ContinueWith&lt;bool&gt;(t =&gt; 
          { 
              return currentItem.ProcessItem(); 
          }, processCancelTokenSource.Token);
  
      // Update UI after each task completes
      currentTask.ContinueWith(t =&gt;
      {
          if (t.IsCanceled)
              currentItem.State = State.Cancelled;
          else
          {
              if (t.Result)
                  currentItem.State = State.Complete;
              else
                  currentItem.State = State.Failed;
          }
      },TaskScheduler.FromCurrentSynchronizationContext());
  }
  </code></pre>
  
  <p>Now, I am using a <code>CancellationToken</code> to cancel the processing of the queue (there is a "Cancel Processing" button).  </p>
  
  <p>The problem is that this does not cancel the Task which is currently being executed.  If <code>CancellationTokenSource.Cancel()</code> is called, then all the tasks waiting to be executed in the queue will be cancelled and their item's <code>currentItem.State</code> will be set to <code>State.Cancelled</code>, which is correct.  The problem is that the task which was executing at the time of the cancellation will continue to execute until it is finished and then be set to <code>State.Complete</code> or <code>State.Failed</code>.  This is not ideal for two reasons: (1) the task is still running after the cancellation, (2) the state is not set to <code>State.Cancelled</code> because <code>t.IsCanceled</code> is not true.</p>
  
  <p>Is there a way for me to safely cancel/stop the currently executing Task? </p>