Back to Devexpress

CRR0038 - The CancellationToken parameter is never used

coderushforroslyn-119693-static-code-analysis-analyzers-library-crr0038-the-cancellation-token-parameter-is-never-used.md

latest2.0 KB
Original Source

CRR0038 - The CancellationToken parameter is never used

  • Feb 28, 2025

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

csharp
async Task ProcessFileAsync(string path, CancellationToken cancellationToken)
{
    var lines = await File.ReadAllLinesAsync(path).ConfigureAwait(false);
    foreach (var line in lines)
    {
        //...
    }    
}
vb
Private Async Function ProcessFileAsync(ByVal path As String, ByVal cancellationToken As CancellationToken) 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 is 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