Usage of Cancellation Token

2019-02-26 22:17发布

I am trying learn how to cancel Task using cancellation token. Here, I have written a UnitTest for it but i am not getting the way it is working.

[TestMethod]
public async Task Task_should_not_run_when_token_cancelled_before_its_call()
{
    var cts = new CancellationTokenSource();
    var token = cts.Token;

    cts.Cancel();

    Debug.WriteLine("Calling Cancellable Method".ToUpper());
    try
    {
        await CheckMeCalled(token);
    }
    catch (Exception expException)
    {

    }
}

private async Task CheckMeCalled(CancellationToken ct)
{
    Debug.WriteLine("Before task delayed".ToUpper());
    await Task.Delay(5000);
    Debug.WriteLine("After task delayed".ToUpper());
}

In the above Test i Called the cts.Cancel() before calling the CheckMeCalled method. so it should not run as it canceled. But it is running to the full extent.

I have read somewhere that

If Task is not running and you cancel it then it will not go to a running state instead it go for canceled state when you call it.

But it is not seems to be happening here. can somebody explain it to me ? any help is appreciated. cheers :)

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-26 22:19

You have to manually check the token to see if a cancellation was requested.

With:

ct.IsCancellationRequested
查看更多
祖国的老花朵
3楼-- · 2019-02-26 22:29

The quote you added was related to creating a new Task via Task.Run or Task.Factory.Startnew. When passing a CancellationToken to your method, you have to actively check the token before running

private async Task CheckMeCalled(CancellationToken ct)
{
    ct.ThrowIfCancellationRequested();
    Debug.WriteLine("Before task delayed".ToUpper());
    await Task.Delay(5000, ct);
    Debug.WriteLine("After task delayed".ToUpper());
}

Here is a quote by Stephan Toub regarding cancellation token and Task:

If the token has cancellation requested prior to the Task starting to execute, the Task won't execute. Rather than transitioning to Running, it'll immediately transition to Canceled. This avoids the costs of running the task if it would just be canceled while running anyway.

I also recommend reading NET 4 Cancellation Framework for a broad review of the cancellation mechanism by the PFX Team

查看更多
登录 后发表回答