coderushforroslyn-119693-static-code-analysis-analyzers-library-crr0038-the-cancellation-token-parameter-is-never-used.md
This analyzer detects asynchronous methods that do not use the passed CancellationToken parameter.
async Task ProcessFileAsync(string path, CancellationToken cancellationToken)
{
var lines = await File.ReadAllLinesAsync(path).ConfigureAwait(false);
foreach (var line in lines)
{
//...
}
}
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.
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();
//...
}
}
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