Task.ContinueWith() executing but Task Status is still "Running"
18
Consider the following code:
MyTask = LongRunningMethod(progressReporter, CancelSource.Token)
.ContinueWith(e =>
{ Log.Info("OnlyOnCanceled"); },
default,
TaskContinuationOptions.OnlyOnCanceled,
TaskScheduler.FromCurrentSynchronizationContext())
.ContinueWith(e =>
{ Log.Info("OnlyOnFaulted"); },
default,
TaskContinuationOptions.OnlyOnFaulted,
TaskScheduler.FromCurrentSynchronizationContext())
.ContinueWith(e =>
{ Log.Info("OnlyOnRanToCompletion"); },
default,
TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.FromCurrentSynchronizationContext())
.ContinueWith(e =>
{ Log.Info("None"); },
default,
TaskContinuationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
- When I cancel the task using the provided CancelSource, output is:
OnlyOnCanceledNoneas expected.- When LongRunningMethod throws an Exception output is:OnlyOnFaultedNoneas expected.- WhenLongRunningMethodcompletes output is:Noneso theContinueWithwithTaskContinuationOptions.OnlyOnRanToCompletionis not executed as I would expect.
I checked MyTask.Status in the last ContinueWith branch and it is still Running. So with that in mind, I would expect OnlyOnRanToCompletion to be skipped. The question is, why is the Status still Running? Looking at the debug output, I can see that LongRunningMethod ran to the end.