From 5e94f3d8c465574a4b9c757ac13041d3e40d2930 Mon Sep 17 00:00:00 2001 From: kurnakovv Date: Thu, 9 Oct 2025 14:00:26 +0900 Subject: [PATCH] Add code example for CA2008 rule (#49035) --- .../code-analysis/quality-rules/ca2008.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca2008.md b/docs/fundamentals/code-analysis/quality-rules/ca2008.md index 68218e9564457..fd0ead2ab8e15 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca2008.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca2008.md @@ -9,6 +9,8 @@ helpviewer_keywords: - CA2008 author: gewarren ms.author: gewarren +dev_langs: +- CSharp --- # CA2008: Do not create tasks without passing a TaskScheduler @@ -42,6 +44,44 @@ For further information and detailed examples, see [New TaskCreationOptions and To fix violations, call the method overload that takes a and explicitly pass in or 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.