Metadata

ID: csharp-best-practices/incorrect-complete-options

Language: C#

Severity: Error

Category: Error Prone

Description

The TaskContinuationOptions enumeration is used to control the behavior of a task that is created to handle the completion of another task. However, using TaskContinuationOptions in a TaskCompletionSource is not recommended and can lead to unexpected behavior.

The importance of this rule is that it helps to avoid potential threading issues and ensures that your asynchronous code runs as expected. Misusing TaskContinuationOptions could lead to your continuation running on an unexpected thread, causing potential problems with thread-safety and synchronization.

To adhere to this rule and good coding practices, you should use TaskCreationOptions instead of TaskContinuationOptions when creating a new TaskCompletionSource.

Non-Compliant Code Examples

var tcs = new TaskCompletionSource<int>(TaskContinuationOptions.RunContinuationsAsynchronously);

Compliant Code Examples

var tcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security