title: Who canceled my Task?
body: <p>My C# Task is getting canceled, but not by me. I don't get a stacktrace and I can't figure out where the problem occurs.</p>
  
  <p>My task invocation looks like this:</p>
  
  <pre><code>var t = Task&lt;Boolean&gt;.Factory.StartNew(() =&gt;
      {
          Boolean bOk = DoSomthingImportant();
          return bOk;
      }, TaskCreationOptions.AttachedToParent)
      .ContinueWith&lt;Boolean&gt;((theTask) =&gt;
      {
          var reason = theTask.IsCanceled ? "it was canceled" : "it faulted";
          Debug.WriteLine("Error: Task ended because " + reason + ".");
          ... log the exception to one of my objects...
          return false;
      }, TaskContinuationOptions.NotOnRanToCompletion);
  </code></pre>
  
  <p>I want the continuation task to run if the task faulted or was canceled, but not if it ran okay.
  The continuation is never executed.</p>
  
  <p>Later on my program catches an AggregateException which is wrapping a TaskCanceledException.</p>
  
  <p>My other major interaction with my tasks is to call WaitAny(taskArray, timeout) until I have no more tasks to start, then call WaitAll with no timeout until the last task is done.</p>
  
  <p>Could WaitAny with a timeout cause a cancellation? Why didn't my continuation get called?</p>
  
  <p>This is only my second brush with the Task library, so I am clueless.</p>
  
  <p>UPDATE:</p>
  
  <p>I found this SO question: How to propagate a Task's Canceled status to a continuation task.
  One error in my code above (but not the cause of the Cancelation) is that I assumed that the Continuation tasks status was the same as the original task's status. In fact you have to do some work to get the one from the other, as the other post describes.</p>
  
  <p>UPDATE 2:</p>
  
  <p>Brian: Thanks for the documentaion reference. I had searched high and low for alternate causes of a Task being canceled, but missed these words:</p>
  
  <blockquote>
    <p>"If you are waiting on a Task that transitions to the Canceled state,
    a Task (wrapped in an AggregateException) is manufactured and thrown.
    Note that this exception indicates successful cancellation instead of
    a faulty situation. Therefore, the Task's Exception property returns
    null."</p>
  </blockquote>