Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct use of CancellationToken

This is my situation:

    private CancellationTokenSource cancellationTokenSource;
    private CancellationToken cancellationToken;

    public IoTHub()
    {
        cancellationTokenSource = new CancellationTokenSource();
        cancellationToken = cancellationTokenSource.Token;

        receive();
    }

    private void receive()
    {
        eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, iotHubD2cEndpoint);
        var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;

        foreach (string partition in d2cPartitions)
        {
            ReceiveMessagesFromDeviceAsync(partition, cancellationToken);
        }
    }

    private async Task ReceiveMessagesFromDeviceAsync(CancellationToken ct)
    {
        var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.UtcNow);

        while (true)
        {
            if(ct.IsCancellationRequested)
            {
                break;
            }

            EventData eventData = await eventHubReceiver.ReceiveAsync();
            if (eventData == null) continue;

            string data = Encoding.UTF8.GetString(eventData.GetBytes());

            // Javascript function with Websocket
            Clients.All.setMessage(data);
        }
    }

    public void cancelToken()
    {
      cancellationTokenSource.Cancel();
    }

The Task will not be cancelled, when calling the cancelToken method. How come?

I have read the Microsoft guide, an other Stackoverflow questions about Task cancellation.

But still have difficulty using them correctly.

like image 638
BlueCastle Avatar asked Mar 23 '16 08:03

BlueCastle


People also ask

When should I use CancellationToken?

So CancellationToken can be used to terminate a request execution at the server immediately once the request is aborted or orphan. Here we are going to see some sample code snippets about implementing a CancellationToken for Entity FrameworkCore, Dapper ORM, and HttpClient calls in Asp.

What can you do with CancellationToken?

A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource. Token property.

What is default for CancellationToken?

You can also use the C# default(CancellationToken) statement to create an empty cancellation token. Two empty cancellation tokens are always equal.

Should I call ThrowIfCancellationRequested?

Yes, you are supposed to call ThrowIfCancellationRequested() manually, in the appropriate places in your code (where appropriate is determined by you as a programmer). Consider the following example of a simple job processing function that reads jobs from a queue and does stuff with them.


1 Answers

You can consider CancellationToken like a flag, indicating if a cancellation signal is received. Thus:

while (true)
{
    //you check the "flag" here, to see if the operation is cancelled, correct usage
    if(ct.IsCancellationRequested)
    {
        break;
    }

    //your instance of CancellationToken (ct) can't stop this task from running
    await LongRunningTask();
}

If you want LongRunningTask to be cancelled, you should use CancellationToken inside the task body and check it when necessary, like this:

async Task LongRunningTask()
{
    await DoPrepareWorkAsync();

    if (ct.IsCancellationRequested)
    {
        //it's cancelled!
        return;
    }

    //let's do it
    await DoItAsync();

    if (ct.IsCancellationRequested)
    {
        //oh, it's cancelled after we already did something!
        //fortunately we have rollback function
        await RollbackAsync();
    }
}
like image 86
Cheng Chen Avatar answered Sep 24 '22 06:09

Cheng Chen