Back to Devexpress

CRR0035 - No CancellationToken parameter in the asynchronous method

coderushforroslyn-119690-static-code-analysis-analyzers-library-crr0035-no-cancellation-token-parameter-in-the-asynchronous-method.md

latest1.9 KB
Original Source

CRR0035 - No CancellationToken parameter in the asynchronous method

  • Feb 28, 2025

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

csharp
async Task ProcessFileAsync(string path)
{
    var lines = await File.ReadAllLinesAsync(path).ConfigureAwait(false);
    foreach (var line in lines)
    {
        //...
    }  
}
vb
Private Async Function ProcessFileAsync(ByVal path As String) As Task
    Dim lines = Await File.ReadAllLinesAsync(path).ConfigureAwait(False)

    For Each line In lines
    Next
        '...    
End Function

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.

csharp
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();
        //...
    }    
}
vb
Private Async Function ProcessFileAsync(ByVal path As String, ByVal cancellationToken As CancellationToken) As Task
    cancellationToken.ThrowIfCancellationRequested()
    Dim lines = Await File.ReadAllLinesAsync(path, cancellationToken).ConfigureAwait(False)

    For Each line In lines
        cancellationToken.ThrowIfCancellationRequested()
    Next
       '...    
End Function

See Also

Suppress Analyzers