Skip to main content

CRR0035 - No CancellationToken parameter in the asynchronous method

This analyzer detects asynchronous methods that do not accept the CancellationToken parameter.

async Task ProcessFileAsync(string path)
{
    var lines = await File.ReadAllLinesAsync(path).ConfigureAwait(false);
    foreach (var line in lines)
    {
        //...
    }  
}

All asynchronous methods should accept the CancellationToken parameter and use it to cancel a task if task cancellation was requested. Refer to the Task Cancellation article for more information.

async Task ProcessFileAsync(string path, CancellationToken cancellationToken)
{
    cancellationToken.ThrowIfCancellationRequested();
    var lines = await File.ReadAllLinesAsync(path, cancellationToken).ConfigureAwait(false);
    foreach (var line in lines)
    {
        cancellationToken.ThrowIfCancellationRequested();
        //...
    }    
}