You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fundamentals/code-analysis/quality-rules/ca2008.md
+40Lines changed: 40 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,8 @@ helpviewer_keywords:
9
9
- CA2008
10
10
author: gewarren
11
11
ms.author: gewarren
12
+
dev_langs:
13
+
- CSharp
12
14
---
13
15
# CA2008: Do not create tasks without passing a TaskScheduler
14
16
@@ -42,6 +44,44 @@ For further information and detailed examples, see [New TaskCreationOptions and
42
44
43
45
To fix violations, call the method overload that takes a <xref:System.Threading.Tasks.TaskScheduler> and explicitly pass in <xref:System.Threading.Tasks.TaskScheduler.Default%2A> or <xref:System.Threading.Tasks.TaskScheduler.Current%2A> to make the intent clear.
44
46
47
+
## Example
48
+
49
+
```csharp
50
+
// This code violates the rule.
51
+
varbadTask=Task.Factory.StartNew(
52
+
() =>
53
+
{
54
+
// ...
55
+
}
56
+
);
57
+
badTask.ContinueWith(
58
+
t=>
59
+
{
60
+
// ...
61
+
}
62
+
);
63
+
64
+
// This code satisfies the rule.
65
+
vargoodTask=Task.Factory.StartNew(
66
+
() =>
67
+
{
68
+
// ...
69
+
},
70
+
CancellationToken.None,
71
+
TaskCreationOptions.None,
72
+
TaskScheduler.Default
73
+
);
74
+
goodTask.ContinueWith(
75
+
t=>
76
+
{
77
+
// ...
78
+
},
79
+
CancellationToken.None,
80
+
TaskContinuationOptions.None,
81
+
TaskScheduler.Default
82
+
);
83
+
```
84
+
45
85
## When to suppress warnings
46
86
47
87
This warning is intended primarily for libraries, where the code may be executed in arbitrary environments and where code shouldn't make assumptions about the environment or how the caller of the method may be invoking or waiting on it. It may be appropriate to suppress the warning for projects that represent application code rather than library code.
0 commit comments