Who canceled my task?

My C # task is canceled, but not by me. I am not getting stacktrace and I cannot figure out where the problem is.

My task call looks like this:

var t = Task<Boolean>.Factory.StartNew(() => { Boolean bOk = DoSomthingImportant(); return bOk; }, TaskCreationOptions.AttachedToParent) .ContinueWith<Boolean>((theTask) => { 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); 

I want the continuation task to be executed if the task was crashing or canceled, but not if it was working fine. Continuation is never executed.

Later, my program catches an AggregateException that throws a TaskCanceledException.

Another important interaction with my tasks is to call WaitAny (taskArray, timeout) until I have more tasks to run, and then call WaitAll without a timeout until the last task is completed.

Can a WaitAny with a timeout cause a cancellation? Why didn’t my sequel provoke?

This is just my second brush with a task library, so I don’t know.

UPDATE:

I found this SO question: how to distribute the status "Canceled task" to the continuation task. One error in my code above (but not the reason for the cancellation) is that I assumed that the status of the continuation tasks is the same as the status of the original task. In fact, you need to do some work to get it from another, as another post describes.

UPDATE 2:

Brian: Thanks for the documentation link. I searched high and low for alternative reasons for canceling the task, but missed these words:

"If you expect the task to go into a canceled state, the Task (wrapped in an AggregateException) is thrown and thrown. Please note that this exception indicates a successful cancellation instead of an error situation. Thus, the" Task exception "property returns zero."

+8
c # task-parallel-library
source share
1 answer

You are expecting a continuation, and since the original task has completed, the continuation task has been canceled. This behavior is described in the documentation .

+14
source share

All Articles