Pattern: Missing use of ThrowIfCancellationRequested
Issue: -
Conditional statements that check IsCancellationRequested
before throwing OperationCanceledException
can be replaced by CancellationToken.ThrowIfCancellationRequested()
.
Example of incorrect code:
public void TestMethod(CancellationToken token)
{
if (token.IsCancellationRequested)
throw new OperationCanceledException();
}
Example of correct code:
public void TestMethod(CancellationToken token)
{
token.ThrowIfCancellationRequested();
}