Skip to content

Files

Latest commit

 

History

History
30 lines (21 loc) · 697 Bytes

File metadata and controls

30 lines (21 loc) · 697 Bytes

Pattern: Missing use of ThrowIfCancellationRequested

Issue: -

Description

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();
}

Further Reading