csharp-async-patterns

Implements C# async/await patterns including Task, ValueTask, async streams, and cancellation.

Updated May 22, 2026
One-click install
npx skills add https://github.com/viniciuscs84/sdd-toolkit --skill csharp-async-patterns-viniciuscs84
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csharp-async-patterns
Source: https://github.com/viniciuscs84/sdd-toolkit/tree/main/skills/csharp-async-patterns
Command: npx skills add https://github.com/viniciuscs84/sdd-toolkit --skill csharp-async-patterns-viniciuscs84

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing asynchronous C# code correctly is error-prone: blocking on async code causes deadlocks, async void swallows exceptions, and missing cancellation support makes long-running operations unresponsive. This Skill provides proven patterns for async/await, Task composition, ValueTask optimization, async streams, and cancellation so you avoid these pitfalls. ## Core Features & Use Cases - Async/Await Fundamentals: Correct method signatures, Task and Task<T> creation, and sequential versus parallel composition with Task.WhenAll and Task.WhenAny. - Performance & Safety Patterns: ValueTask for hot paths, ConfigureAwait(false) in library code, deadlock prevention, and structured exception handling including AggregateException. - Streaming & Cancellation: IAsyncEnumerable<T> async streams with filtering and buffering, CancellationToken propagation, linked tokens, and ASP.NET Core background services. - Use Case: You are building an ASP.NET Core API that fetches data from multiple sources concurrently with a timeout. Use this Skill to compose tasks with Task.WhenAll, apply CancellationToken with a CancellationTokenSource timeout, and stream results via IAsyncEnumerable. ## Quick Start Ask the AI to review or write your C# asynchronous code using the async patterns in this skill, for example to convert a blocking method into a properly cancellable async Task-based implementation.

Frequently Asked Questions about csharp-async-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I run multiple async tasks in parallel in C#?

Use Task.WhenAll to start multiple async operations concurrently and await their combined completion. Start each task without awaiting, then await Task.WhenAll(task1, task2, task3) and read each task's Result afterward.

When should I use ValueTask instead of Task in C#?

Use ValueTask when a method frequently completes synchronously, such as cache hits or buffered reads, to avoid heap allocations. Never await a ValueTask more than once or store it for later use; convert it with AsTask() if needed.

Why does my async C# code deadlock with .Result or .Wait()?

Blocking on async code with .Result or .Wait() deadlocks when a synchronization context exists, such as in UI apps, because the continuation cannot resume on the blocked thread. Fix it by using async all the way or ConfigureAwait(false) in library code.

Should I use ConfigureAwait(false) in ASP.NET Core?

ASP.NET Core has no synchronization context, so ConfigureAwait(false) is safe and optional there. It remains important in reusable library code that may be consumed by UI or legacy ASP.NET applications.

How do I add cancellation to an async method in C#?

Accept a CancellationToken parameter with a default value, pass it to downstream async calls, and call ThrowIfCancellationRequested in loops. Callers create a CancellationTokenSource with a timeout or cancel it manually.

When is async void acceptable in C#?

Async void is only acceptable for event handlers such as button clicks, where the signature is fixed. Everywhere else use async Task, because async void methods cannot be awaited and their exceptions crash the process.