- Home
- Skills
- Aaronontheweb
- Dotnet Skills
- Csharp Concurrency Patterns
csharp-concurrency-patterns_skill
- Shell
643
GitHub Stars
2
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill aaronontheweb/dotnet-skills --skill csharp-concurrency-patterns- advanced-concurrency.md10.9 KB
- SKILL.md8.8 KB
Overview
This skill helps .NET developers choose the right concurrency abstraction for common scenarios, from async/await for I/O to Channels for producer/consumer patterns and Akka.NET for stateful entity management. It emphasizes avoiding locks and shared mutable state unless unavoidable, and shows a progressive escalation of tools as complexity grows.
How this skill works
The skill inspects your concurrency goal (I/O wait, CPU parallelism, producer/consumer, stream processing, UI events, or many independent entities) and recommends the simplest effective abstraction. It explains trade-offs and patterns: async/await for I/O, Parallel.ForEachAsync for CPU-bound work, System.Threading.Channels for buffering/backpressure, Akka.NET Streams for complex server pipelines, Rx for UI/event composition, and Akka.NET Actors for stateful entity isolation and supervision.
When to use it
- Waiting for I/O (HTTP, DB, file) — use async/await
- Processing CPU-bound collections with controlled parallelism — use Parallel.ForEachAsync
- Decoupling producers and consumers with backpressure — use System.Threading.Channels
- Complex server-side stream requirements: batching, throttling, backpressure — use Akka.NET Streams
- UI event composition: debounce, throttle, combine streams — use Reactive Extensions (Rx)
- Managing many independent stateful entities or implementing supervisors and state machines — use Akka.NET Actors
Best practices
- Start simple: default to async/await and escalate only for specific unmet needs
- Avoid shared mutable state via immutability, message passing, or actor isolation
- Prefer System.Collections.Concurrent and Channels before resorting to locks
- Always accept CancellationToken and avoid blocking on async code (.Result/.Wait())
- Choose tools by property: backpressure and ordering (Akka Streams), UI event time semantics (Rx), isolated state and supervision (Akka Actors)
Example use cases
- Load dashboard data concurrently with Task.WhenAll and proper cancellation
- Implement a bounded background work queue with Channel<T> and multiple consumers
- Process images CPU-bound with Parallel.ForEachAsync and a bounded degree of parallelism
- Build a server pipeline that batches events by size or time using Akka.NET Streams
- Handle search-as-you-type in a UI with Rx Throttle and DistinctUntilChanged
- Model order lifecycle with an entity-per-actor pattern and Become() state transitions
FAQ
Use locks rarely: for short-lived low-level critical sections or when redesign to avoid shared state is impossible. Prefer Concurrent collections, Channels, or actors first.
Can Rx be used on the server?
Rx works for server-side event composition but lacks built-in backpressure and supervision that Akka.NET Streams provides; choose Akka Streams for heavy server pipelines.