csharp-async-patterns

Apply correct async/await patterns to solve C# asynchronous programming issues.

10|Updated Nov 21, 2025
One-click install
npx skills add https://github.com/creator-hian/claude-code-plugins --skill csharp-async-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csharp-async-patterns
Source: https://github.com/creator-hian/claude-code-plugins/tree/main/csharp-plugin/skills/csharp-async-patterns
Command: npx skills add https://github.com/creator-hian/claude-code-plugins --skill csharp-async-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance on modern C# asynchronous programming, helping developers avoid common pitfalls like deadlocks, unhandled exceptions, and resource waste. It promotes best practices for building responsive and efficient applications, ensuring reliable async operations.

Core Features & Use Cases

  • Async/Await Best Practices: Learn proper CancellationToken usage, ConfigureAwait guidance, and effective error handling in async code.
  • Task Composition: Master parallel execution, sequential dependencies, and "first wins" patterns for complex async workflows.
  • Performance Optimization: Utilize ValueTask for allocation-free async operations in performance-critical code paths.
  • Use Case: Refactor a legacy synchronous data loading service to be fully asynchronous. Use this skill to implement cancellation, timeouts, and retry logic, ensuring the application remains responsive and handles transient network failures gracefully.

Quick Start

Refactor this synchronous method to be asynchronous, including cancellation support: public string LoadData() { /* ... */ }

Frequently Asked Questions about csharp-async-patterns

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

FAQPage Schema
How do I implement async/await patterns correctly in C# to avoid deadlocks and unhandled exceptions?

Async/await in C# enables non-blocking asynchronous operations by letting methods pause and resume. Use ConfigureAwait(false) in library code, propagate CancellationTokens through call chains, avoid async void except in event handlers, and always await Task results. This prevents deadlocks, resource leaks, and improves application responsiveness.

What's the best way to handle cancellation in C# async operations?

Cancellation in async code uses CancellationToken passed through method signatures and checked within async methods. Operations like Task.Delay and HttpClient respect the token natively. Propagate tokens explicitly from entry points downward, and throw OperationCanceledException when signaled. This ensures clean, coordinated shutdown of long-running async workflows.

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

ValueTask reduces heap allocations in performance-critical async code paths where operations frequently complete synchronously. Use it for high-frequency async methods that often return completed results. Regular Task remains better for most library code and complex async workflows where the allocation overhead is negligible.

How do I refactor synchronous C# code to async with proper error handling and timeouts?

Convert sync methods to async by marking them with async, replacing blocking calls with await equivalents, and adding CancellationToken parameters. Implement timeouts using CancellationTokenSource.CancelAfter or Task.WaitAsync. Wrap operations in try-catch to handle TaskCanceledException and other async exceptions robustly.

What are common mistakes to avoid when composing multiple async tasks in C#?

Avoid async void outside event handlers, omitting ConfigureAwait in libraries, and forgetting CancellationToken propagation. Never use Task.Result or Task.Wait on async methods—await instead. Use Task.WhenAll for parallel execution and sequential await for dependencies. These mistakes cause deadlocks, context issues, and unhandled exceptions.

Does C# async/await work with retry and timeout strategies in .NET applications?

Yes. Implement retries by wrapping await calls in loops with exponential backoff. Add timeouts using CancellationTokenSource, Task.Delay between attempts, or Task.WaitAsync. Combine CancellationToken propagation with exception handling to enforce timeouts and retry limits across network calls and I/O operations.