ContinueWith a cancelled Task

不羁岁月 提交于 2019-12-19 16:13:24

问题


I have defined the following Task

var t = Task.Factory.StartNew(
    () => LongRunningMethod( cancellationToken ),
    cancellationToken
);

t.ContinueWith(
    Callback,
    cancellationToken,
    TaskContinuationOptions.None,
    TaskScheduler.FromCurrentSynchronizationContext()
);

Inside the LongRunningMethod, I check if the cancellation token has a cancellation requested, and if so, I return from the method. That much works fine.

However, the Callback does not get called in this scenario. The callback does get called if I replace the second line above with

t.ContinueWith(
    x => Callback( x, cancellationToken ),
    TaskScheduler.FromCurrentSynchronizationContext()
);

In this situation, the task still thinks it ran to completion.

Why doesn't the first call work? I was under the impression that TaskContinuationOptions.None means that the callback will get called regardless of the state of the thread.

I am cancelling the Task by calling:

_cancellationTokenSource.Cancel();

On a somewhat related note, having to pass around cancellation tokens seems like a major design flaw of the Task library.


回答1:


Your continuation task takes a CancellationToken which you cancel. This means that the continuation task, while unstarted, is being cancelled. Don't pass that token to thing which you don't want to cancel.

CancellationToken is meant to cancel the whole graph of actions at once. You can exclude stuff from cancellation by not passing the token.



来源:https://stackoverflow.com/questions/11892315/continuewith-a-cancelled-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!