> Markdown version of [/videos/447-asynchronicity-and-multithreading-in-c](https://www.wearedevelopers.com/videos/447-asynchronicity-and-multithreading-in-c). Every page supports `.md` or `Accept: text/markdown`. Links point to the HTML versions so they work for humans too. Agent guide: [/agents.md](https://www.wearedevelopers.com/agents.md). --- # Asynchronicity and multithreading in C# Are you still manually spawning threads in C#? Discover why blocking threads kills performance and how mastering modern async/await patterns maximizes server throughput. - **Speakers:** Grzegorz Lang - **Event:** World Congress 2022 - **Published:** June 15, 2022 - **Duration:** 26:34 - **URL:** https://www.wearedevelopers.com/videos/447-asynchronicity-and-multithreading-in-c ## Summary This presentation explores the critical mechanisms for handling CPU-intensive and I/O-bound operations in C#, breaking down the distinctions between physical and logical threads. Because context switching between threads is expensive, developers are urged to avoid manually spawning threads and instead rely on the .NET ThreadPool via abstractions like `Task.Run`. A foundational rule of multithreading in .NET is to limit the number of logical threads to physical worker capacity to prevent degrading application performance. Transitioning from CPU-intensive work to I/O-bound operations, the `async`/`await` pattern takes center stage. By compiling into underlying state machines, `async`/`await` allows the calling thread to process other work rather than idly waiting on databases or filesystems. The core insight here is to "never ever block threads" by misusing `.Result` or synchronous waits. Developers are also strongly advised to avoid `async void` to ensure proper execution observability and exception handling. For highly optimized, allocation-free operations in hot paths, `ValueTask` is introduced as a struct-based alternative to `Task`, though it comes with strict usage rules like avoiding concurrent execution. Finally, the talk demystifies `SynchronizationContext` and `TaskScheduler`, showing how they collaborate to safely route operations back to specific environments, such as updating UI components in Blazor. Utilizing `ConfigureAwait(false)` is highlighted as an essential practice for writing environment-agnostic library code. Ultimately, while concurrency is notoriously difficult to debug and "easy to mess up," mastering these abstractions and cancellation tokens leads to highly responsive user interfaces and maximized server throughput. **Keywords:** c# multithreading, .net asynchronicity, threadpool management, task.run abstraction, async/await state machines, synchronizationcontext, taskscheduler, cancellation tokens, valuetask structs, configureawait(false), io-bound operations, cpu-intensive processing, async void avoidance, thread blocking pitfalls, visual studio parallel stacks ## Chapters 1. **Multithreading basics and physical versus logical threads** (00:47) — Multithreading handles CPU-intensive work by delegating calculations to limited physical and logical worker threads. 1. **Managing thread allocation using the dotnet thread pool** (04:01) — The dotnet thread pool prevents system overload by managing thread lifecycle and task abstractions. 1. **Avoiding blocked threads during ongoing background calculations** (05:30) — Using synchronous result properties freezes physical threads instead of safely continuing execution after task completion. 1. **Recommended abstractions for starting tasks and background work** (07:02) — Defaulting to modern task abstractions prevents common thread initialization mistakes and configuration errors. 1. **Implementing cancellation tokens to terminate long running operations** (08:28) — Cancellation tokens safely interrupt background services and clean up allocated resources upon request. 1. **Synchronizing background calculations with user interface rendering contexts** (09:55) — Synchronization contexts ensure that background thread calculations safely update interface components on the primary environment thread. 1. **Using task schedulers to route background thread execution** (12:06) — Task schedulers collaborate with environment contexts to determine where and how asynchronous operations execute. 1. **Avoiding locked resources during database and network requests** (13:59) — Asynchronous patterns release threads to the pool during input and output delays instead of waiting idle. 1. **Compiler state machines and asynchronous concurrent task execution** (17:14) — The compiler transforms asynchronous methods into state machines that easily resume task execution upon network completion. 1. **Preventing hidden errors by avoiding asynchronous void return types** (19:23) — Asynchronous methods lacking a measurable return type swallow exceptions and prevent accurate execution tracking. 1. **Reducing heap allocations by implementing value task structures** (20:08) — Value tasks optimize execution paths by avoiding heap allocations during highly frequent concurrent calculations. 1. **Modifying asynchronous synchronization context configurations for software libraries** (21:17) — Disabling context capture in background tasks ensures agnostic library environments without unnecessary interface thread coupling. 1. **Clarifying asynchronous patterns and modern framework implementation differences** (22:43) — Understanding synchronization context changes in modern frameworks helps mitigate common thread blocking mistakes during implementation. ## Related Moments - [Awaiting async void methods with custom task schedulers](https://www.wearedevelopers.com/videos/117-hacking-c-from-the-inside-how-to-do-anything-in-net) (from "Hacking C# from the inside - how to do anything in NET") - [Enforcing performant threading models at the framework level](https://www.wearedevelopers.com/videos/1445-lynx-native-for-more) (from "Lynx: Native for More") - [Shift away from traditional threads and the asynchronous era](https://www.wearedevelopers.com/videos/1973-beyond-loom-practical-patterns-for-virtual-threads-structured-concurrency-and-reactive-interop) (from "Beyond Loom: Practical Patterns for Virtual Threads, Structured Concurrency and Reactive Interop") - [Audience Q&A on arrow functions and multithreading](https://www.wearedevelopers.com/videos/306-javascript-the-grumpy-parts) (from "JavaScript the Grumpy Parts") - [Managing asynchronous operations and concurrent multithreading with isolates](https://www.wearedevelopers.com/videos/955-dart-a-language-believed-dead-experiences-a-new-bloom) (from "Dart - a language believed dead, experiences a new bloom") - [Implementing cooperative multitasking in modern programming languages](https://www.wearedevelopers.com/videos/690-coroutine-explained-yet-again-60-years-later) (from "Coroutine explained yet again 60 years later") ## Related Articles - [The Concept of Concurrency & Multithreading: What Are They And How They Function](https://www.wearedevelopers.com/magazine/89-the-concept-of-concurrency-multithreading-what-are-they-and-how-they-function) - [Dev Digest 108 - Git off my cloud!](https://www.wearedevelopers.com/magazine/407-dev-digest-108-git-off-my-cloud) - [Get Old, go Slow, Write Code!](https://www.wearedevelopers.com/magazine/81-get-old-go-slow-write-code) - [Dev Digest 132 - Binging WADFlix?](https://www.wearedevelopers.com/magazine/473-dev-digest-132-binging-wadflix) ## Related Jobs - [Senior Software Engineer,Billing](https://www.wearedevelopers.com/jobs/ext/1991843-senior-software-engineer-billing) at **GitHub** - [Principal Software Engineer, Identity](https://www.wearedevelopers.com/jobs/ext/1469181-principal-software-engineer-identity) at **GitHub** - [Senior Software Engineer, Fraud](https://www.wearedevelopers.com/jobs/ext/1280398-senior-software-engineer-fraud) at **Twilio** - [Principal Software Engineer, Enterprise AI Platform](https://www.wearedevelopers.com/jobs/ext/1467292-principal-software-engineer-enterprise-ai-platform) at **GitHub** - [Senior Software Engineer, Client Apps Platform](https://www.wearedevelopers.com/jobs/ext/1773893-senior-software-engineer-client-apps-platform) at **GitHub** - [iOS Engineer, Swift/SwiftUI/Combine (B2C Broker)](https://www.wearedevelopers.com/jobs/ext/1611832-ios-engineer-swift-swiftui-combine-b2c-broker) at **Bitpanda**