csharp-concurrency-patterns

Guides selection of .NET concurrency abstractions from async/await to Channels and Akka.NET actors.

Updated Mar 8, 2026
One-click install
npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill csharp-concurrency-patterns-agibuild
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: csharp-concurrency-patterns
Source: https://github.com/AGIBuild/dotnet.CI.template/tree/main/.cursor/skills/csharp-concurrency-patterns
Command: npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill csharp-concurrency-patterns-agibuild

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Choosing the wrong concurrency primitive in .NET leads to race conditions, deadlocks, and over-engineered code. This Skill provides a decision framework for picking the right abstraction—async/await, Parallel.ForEachAsync, Channels, Reactive Extensions, Akka.NET Streams, or Akka.NET Actors—based on the actual concurrency problem. ## Core Features & Use Cases - Decision Tree: Maps common scenarios (I/O waits, CPU-bound parallelism, producer/consumer queues, state machines) to the appropriate .NET concurrency tool. - Escalation Path Guidance: Enforces a start-simple philosophy, escalating from async/await to Channels or actors only when a concrete need exists. - Synchronization Primitives Reference: Compares lock, SemaphoreSlim, Interlocked, ConcurrentDictionary, ReaderWriterLockSlim, and SpinLock with usage rules and anti-patterns. - Use Case: When building a background order-processing service, use the Skill to decide between a bounded Channel worker pool and Akka.NET actors, then apply the provided code patterns directly. ## Quick Start Ask the AI which .NET concurrency approach fits your scenario, such as processing a queue of orders with backpressure, and apply the recommended pattern.

Frequently Asked Questions about csharp-concurrency-patterns

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

FAQPage Schema
How do I choose between async/await, Channels, and Akka.NET in .NET?▼

Use async/await for I/O-bound waits, Channels for producer/consumer work queues with backpressure, and Akka.NET actors for stateful entity management or complex state machines. Start with async/await and escalate only when a concrete need appears.

What .NET tool should I use for producer/consumer patterns?▼

System.Threading.Channels is the recommended tool for producer/consumer patterns in .NET. Bounded channels provide backpressure, and multiple consumers can read from one channel to form a worker pool.

Can I use lock inside an async method in C#?▼

No, lock is thread-affine and the continuation after await may resume on a different thread, causing SynchronizationLockException. Use SemaphoreSlim with WaitAsync for async-compatible mutual exclusion instead.

When should I use Akka.NET actors instead of Channels?▼

Use Akka.NET actors when managing state for many independent entities, implementing state machines with the Become pattern, or needing supervision and fault tolerance. Channels suit simpler work queues without per-entity state.

Why does Parallel.ForEachAsync cause race conditions with shared lists?▼

Parallel.ForEachAsync runs iterations concurrently, so adding to a regular List<T> from multiple iterations causes race conditions. Use ConcurrentBag<T> or another thread-safe collection to gather results safely.