|
| 1 | +--- |
| 2 | +title: "CA2250: Use `ThrowIfCancellationRequested`" |
| 3 | +description: "Learn about code analysis rule CA2250: Use `ThrowIfCancellationRequested`" |
| 4 | +ms.date: 05/21/2021 |
| 5 | +ms.topic: reference |
| 6 | +f1_keywords: |
| 7 | +- CA2250 |
| 8 | +- UseCancellationTokenThrowIfCancellationRequested |
| 9 | +helpviewer_keywords: |
| 10 | +- UseCancellationTokenThrowIfCancellationRequested |
| 11 | +- CA2250 |
| 12 | +author: NewellClark |
| 13 | +dev_langs: |
| 14 | +- CSharp |
| 15 | +- VB |
| 16 | +--- |
| 17 | +# CA2250: Use `ThrowIfCancellationRequested` |
| 18 | + |
| 19 | +| | Value | |
| 20 | +|-|-| |
| 21 | +| **Rule ID** |CA2250| |
| 22 | +| **Category** |[Usage](usage-warnings.md)| |
| 23 | +| **Fix is breaking or non-breaking** |Non-breaking| |
| 24 | + |
| 25 | +## Cause |
| 26 | + |
| 27 | +This rule flags conditional statements that check <xref:System.Threading.CancellationToken.IsCancellationRequested> before throwing <xref:System.OperationCanceledException>. |
| 28 | + |
| 29 | +## Rule description |
| 30 | + |
| 31 | +You can accomplish the same thing by calling <xref:System.Threading.CancellationToken.ThrowIfCancellationRequested?displayProperty=nameWithType>. |
| 32 | + |
| 33 | +## How to fix violations |
| 34 | + |
| 35 | +To fix violations, replace the conditional statement with a call to <xref:System.Threading.CancellationToken.ThrowIfCancellationRequested>. |
| 36 | + |
| 37 | +```csharp |
| 38 | +using System; |
| 39 | +using System.Threading; |
| 40 | + |
| 41 | +public void MySlowMethod(CancellationToken token) |
| 42 | +{ |
| 43 | + // Violation |
| 44 | + if (token.IsCancellationRequested) |
| 45 | + throw new OperationCanceledException(); |
| 46 | + |
| 47 | + // Fix |
| 48 | + token.ThrowIfCancellationRequested(); |
| 49 | + |
| 50 | + // Violation |
| 51 | + if (token.IsCancellationRequested) |
| 52 | + throw new OperationCanceledException(); |
| 53 | + else |
| 54 | + DoSomethingElse(); |
| 55 | + |
| 56 | + // Fix |
| 57 | + token.ThrowIfCancellationRequested(); |
| 58 | + DoSomethingElse(); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +```vb |
| 63 | +Imports System |
| 64 | +Imports System.Threading |
| 65 | + |
| 66 | +Public Sub MySlowMethod(token As CancellationToken) |
| 67 | + |
| 68 | + ' Violation |
| 69 | + If token.IsCancellationRequested Then |
| 70 | + Throw New OperationCanceledException() |
| 71 | + End If |
| 72 | + |
| 73 | + ' Fix |
| 74 | + token.ThrowIfCancellationRequested() |
| 75 | + |
| 76 | + ' Violation |
| 77 | + If token.IsCancellationRequested Then |
| 78 | + Throw New OperationCanceledException() |
| 79 | + Else |
| 80 | + DoSomethingElse() |
| 81 | + End If |
| 82 | + |
| 83 | + ' Fix |
| 84 | + token.ThrowIfCancellationRequested() |
| 85 | + DoSomethingElse() |
| 86 | +End Sub |
| 87 | +``` |
| 88 | + |
| 89 | +## When to suppress warnings |
| 90 | + |
| 91 | +It is safe to suppress warnings from this rule. |
| 92 | + |
| 93 | +## See also |
| 94 | + |
| 95 | +- [Usage Warnings](usage-warnings.md) |
0 commit comments