Grzegorz Lang

Asynchronicity and multithreading in C#

Is `task.Result` secretly killing your application's performance? This session reveals the right way to handle async and multithreading in C# for maximum throughput.

Asynchronicity and multithreading in C#
#1about 3 minutes

Understanding multithreading for CPU-intensive work

Multithreading allows CPU-intensive work to run in the background, but creating too many logical threads leads to expensive context switching.

#2about 3 minutes

Using the ThreadPool and Task abstraction

The ThreadPool manages threads for you, and the Task class provides a powerful abstraction for queuing work and retrieving results without blocking.

#3about 2 minutes

Creating and continuing tasks without blocking

Use Task.Run to create and start a hot task in one step, and use the ContinueWith method to process results without blocking the main thread.

#4about 1 minute

Implementing graceful cancellation with CancellationToken

A CancellationToken signals that an operation should be stopped, which can be handled by checking IsCancellationRequested or calling ThrowIfCancellationRequested.

#5about 2 minutes

How SynchronizationContext manages execution environments

SynchronizationContext is an abstraction for an environment, like a UI thread, allowing you to post work back to a specific context from a background thread.

#6about 2 minutes

The role of the TaskScheduler in C#

The TaskScheduler works with the SynchronizationContext to determine where a task's continuation should be executed, such as on the ThreadPool or a specific UI thread.

#7about 4 minutes

Introduction to asynchronicity with async and await

The async and await keywords simplify asynchronous programming for I/O-bound operations by allowing threads to be released instead of blocked while waiting.

#8about 1 minute

Running operations concurrently with Task.WhenAll

Start multiple asynchronous operations without awaiting them immediately and then use Task.WhenAll to wait for all of them to complete concurrently.

#9about 2 minutes

Choosing the right async return type

Avoid using async void, and consider using the allocation-free ValueTask struct instead of Task for performance-critical hot paths, but be aware of its usage limitations.

#10about 1 minute

Using ConfigureAwait to control context capturing

Use ConfigureAwait(false) in library code to prevent capturing the SynchronizationContext, making the library environment-agnostic and avoiding potential deadlocks.

#11about 4 minutes

Q&A: SynchronizationContext, tooling, and challenges

The Q&A session clarifies that frameworks like Blazor still use SynchronizationContext in .NET Core and discusses debugging tools like Visual Studio's Parallel Stacks.

Related jobs
Jobs that call for the skills explored in this talk.
SabIna compys

SabIna compys
Vienna, Austria

Remote
20-100K
Intermediate
JavaScript
.NET
+1

Featured Partners

Related Articles

View all articles
AP
Anto Pranjić
11 Tips to Make The Most Out of Your First World Congress 
The world’s most awesome event for developers is taking place in less than a month! It’s been far too long since the last time we met in person, but that’s only going to make this year’s WeAreDevelopers World Congress a truly special experience. Conf...
11 Tips to Make The Most Out of Your First World Congress 
AP
Anto Pranjić
That was WeAreDevelopers World Congress 2022
Thank you, danke, hvala, merci, gracias, Дякую – we don’t know yet exactly how many languages were spoken here at the City Cube Berlin over the last two days, but thank you 10 000 times to everybody who joined us here for WeAreDevelopers World Congre...
That was WeAreDevelopers World Congress 2022
AP
Anto Pranjić
5 Reasons Why Attending Conferences Matters More Than You Think
It only took us a global pandemic to realize that most of our daily work can be done from home. It’s kind of funny, but things that weren’t meant to be done from home in the past such as attending meetings, managing teams, and even attending events a...
5 Reasons Why Attending Conferences Matters More Than You Think

From learning to earning

Jobs that call for the skills explored in this talk.