이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

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