This is a continuation from this question: Multiple Task Continuation
I have changed my code as in the answer, however now I am receiving TaskCancelledExceptions
You say that SetCancelledHandler
just adds a continuation to the task. I assume that's the same task RunAsync
gets as a parameter although i can't tell by your code how SetCancelledHandler
gets a task to continue on (I assume we're missing some code). Anyways...
You register 3 continuations on a task that will run when the task completes, is canceled and is faulted. Now let's assume the original task ran to completion successfully without being canceled. That means that 2 of your continuations (OnCanceled
and OnFaulted
) will not run because they don't need to be. The way to tell a task to not run in the TPL
is to cancel it, and that happens automatically.
The difference between your 2 code snippets is that in the first one you await
the task continuations, and they get canceled which explains your exception. On the second snippet you don't await the continuations, just the original task that successfully ran to completion.
P.S: I think the second option is more appropriate. You don't need to await
all those continuations. You want to let them run if they need to.
TL;DR: You await
a canceled continuation task. The continuation task, not the original, is the one that throws an exception.