Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca2008.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ helpviewer_keywords:
- CA2008
author: gewarren
ms.author: gewarren
dev_langs:
- CSharp
---
# CA2008: Do not create tasks without passing a TaskScheduler

Expand Down Expand Up @@ -42,6 +44,44 @@ For further information and detailed examples, see [New TaskCreationOptions and

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.

## Example

```csharp
// This code violates the rule.
var badTask = Task.Factory.StartNew(
() =>
{
// ...
}
);
badTask.ContinueWith(
t =>
{
// ...
}
);

// This code satisfies the rule.
var goodTask = Task.Factory.StartNew(
() =>
{
// ...
},
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.Default
);
goodTask.ContinueWith(
t =>
{
// ...
},
CancellationToken.None,
TaskContinuationOptions.None,
TaskScheduler.Default
);
```

## When to suppress warnings

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.
Expand Down