This is a continuation of this question: Multiple continuation of the task
I changed my code as in the answer, but now I get TaskCancelledExceptions when I try to start tasks.
public virtual async Task RunAsync(TaskWithProgress task) { Show(); TaskIsRunning(); await SetCompletedHandler(TaskComplete()); await SetCancelledHandler(TaskCancelled()) await SetFaultedHandler(TaskFaulted()); await task; Close(); }
however, the following code does not. I am a little fixated on why.
public virtual Task RunAsync(TaskWithProgress task) { Show(); TaskIsRunning(); SetCompletedHandler(TaskComplete()); SetCancelledHandler(TaskCancelled()) SetFaultedHandler(TaskFaulted()); return task; }
The calling code basically includes the following:
await progressDialog.RunAsync(task);
Edit:
I do not cancel cancellationtoken anywhere, so I do not see why this throws this exception.
The three SetXXXHandler () methods basically execute the following code with different continuation status:
task.ContinueWith(_ => action(), CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, this.Scheduler);
The stack trace is here:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at FugroDXExt.frmBaseProgressAsync.<RunAsync>d__7.MoveNext() in d:\C#\FugroDXExt\trunk\frmBaseProgressAsync.cs:line 92 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at FCP.Forms.frmProcessing.<mnuApplyCenteredSmoothing_ItemClick>d__34.MoveNext() in d:\C#\FCP\FCP\Forms\frmProcessing.cs:line 578
Close() just closes the form. If I delete this line, the same thing will happen.
Simon source share