SlideShare a Scribd company logo
Async in C#
The good, the
bad and the
ugly
😋😒😭
Hi, I’m Stu
◍ F# |> I ❤
◍ Developer at
◍ 8 years of experience in .NET
◍ Organiser of Bristol F# meetup and DDD South West
◍ Passionate about Open Source
You can find me at @stuartblang
Sponsors
Why an Async
talk?
History of Async
Async/Await introduced
.NET 4.5
C# 5
2011
Async CTP
2010
You are here!
2017
“
It really troubles me how much async, bad
async, really bad async we see in the wild
Kathleen Dollard - .NET Rocks! #1143
Async in C# - The Good, the Bad and the Ugly
The Good
◍Non-blocking waiting
◍There is no thread!
The Bad
◍Not clear what is true async
◍Performance overhead
◍Duplicated code
◍Async can’t go everywhere
◍Risk of async deadlocks
◍Doing it wrong is much worse that not
doing it at all
How does it work?
Compiler generated code
How does it work?
Synchronization Context
What’s that about?
How does it work?
Continuing on Captured Context
Synchronization Context Cont.
Synchronization Context Cont.
WindowsFormsSynchronizationContext
DispatcherSynchronizationContext
Default (ThreadPool) SynchronizationContext
AspNetSynchronizationContext
SynchronizationContext Behaviors
Specific
Thread Used
to Execute
Delegates
Exclusive
(Delegates
Execute One at
a Time)
Ordered
(Delegates
Execute in
Queue Order)
Send May
Invoke
Delegate
Directly
Post May
Invoke
Delegate
Directly
WinForms Yes Yes Yes
If called from
UI thread Never
WPF Yes Yes Yes
If called from
UI thread Never
Default No No No Always Never
ASP.NET No Yes No Always Always
Deadlock
Async in C# - The Good, the Bad and the Ugly
KA-BLAMO!
Async in C# - The Good, the Bad and the Ugly
Async in C# - The Good, the Bad and the Ugly
KA-BLAMO!
Async in C# - The Good, the Bad and the Ugly
Async in C# - The Good, the Bad and the Ugly
ContextFreeTask
vs-threading
Formal definition of async deadlocks
You are susceptible to deadlocks if:
1. You have a current SynchronizationContext that enforces exclusive access;
2. Some code further into your call stack has access to it and can/does:
a. Synchronously block on some async code;
b. Within that async code awaits an incomplete task that does not use
.ConfigureAwait(false), or temporarily removes the context.
If you use .Result, .Wait(), .GetAwaiter().GetResult() you have done a dangerous
thing, and you should be prepared to guard against naughty awaiters (you may not even
control).
Resources
Automated Tools
◍ Microsoft's AsyncPackage (Roslyn Analyzer) -
Async006 - Detects more blocking calls than you
can shake a stick at.
◍ ConfigureAwait.Fody (yuk)
◍ ConfigureAwait Checker for ReSharper
◍ ConfigureAwaitChecker - Roslyn Analyzer + VSIX
◍ DeadlockDetection
Prevention Methods
◍ .ConfigureAwait(false) all the things
◍ ContextFreeTask
◍ Comment the code
◍ null the SynchronizationContext (with
handy helper functions)
◍ async top to bottom (replace our
libraries and framework where we have
to)
◍ Deadlock detection in QA & Prod
◍ Detect deadlocks in unit tests?
◍ Use ASP.NET Core!
Awesome async resources
◍Stephen Cleary
◌ https://blog.stephencleary.com/
◍Anthony Steele
◌ Avoiding basic mistakes in async await
◌ Resynchronising async code
Thanks!Any questions?
You can find me at @stuartblang & stuart.b.lang@gmail.com
👍

More Related Content

PDF
톰캣 #05-배치
PDF
톰캣 #05+a-배치-parallel deployment
ODP
GPars Quick Hits
PDF
톰캣 #05+b-root-deployment
PPTX
C#: Understanding ConfigureAwait(false)
PPTX
End to-end async and await
PPTX
Task parallel library presentation
PDF
Why async matters
톰캣 #05-배치
톰캣 #05+a-배치-parallel deployment
GPars Quick Hits
톰캣 #05+b-root-deployment
C#: Understanding ConfigureAwait(false)
End to-end async and await
Task parallel library presentation
Why async matters

Similar to Async in C# - The Good, the Bad and the Ugly (20)

PPSX
Async-await best practices in 10 minutes
PPTX
C# Async/Await Explained
PPTX
Sync with async
PDF
Using Async in your Mobile Apps - Marek Safar
PPTX
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
PPTX
Concurrency - responsiveness in .NET
PPTX
Training – Going Async
PPTX
History of asynchronous in .NET
PPTX
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
PDF
NYAN Conference: Debugging asynchronous scenarios in .net
PPTX
Windows Phone 8 - 3.5 Async Programming
PDF
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
PDF
Async Await for Mobile Apps
PPTX
The 3 VS Threading Rules
PPTX
Async Programming with C#5: Basics and Pitfalls
PPTX
Deceptive simplicity of async and await
PPTX
Async and Await on the Server
PPTX
Async await
PPTX
Async CTP 3 Presentation for MUGH 2012
PPTX
Asynchronous programming - .NET Way
Async-await best practices in 10 minutes
C# Async/Await Explained
Sync with async
Using Async in your Mobile Apps - Marek Safar
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
Concurrency - responsiveness in .NET
Training – Going Async
History of asynchronous in .NET
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
NYAN Conference: Debugging asynchronous scenarios in .net
Windows Phone 8 - 3.5 Async Programming
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
Async Await for Mobile Apps
The 3 VS Threading Rules
Async Programming with C#5: Basics and Pitfalls
Deceptive simplicity of async and await
Async and Await on the Server
Async await
Async CTP 3 Presentation for MUGH 2012
Asynchronous programming - .NET Way
Ad

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Cloud computing and distributed systems.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
sap open course for s4hana steps from ECC to s4
PDF
cuic standard and advanced reporting.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Approach and Philosophy of On baking technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
Unlocking AI with Model Context Protocol (MCP)
Network Security Unit 5.pdf for BCA BBA.
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Cloud computing and distributed systems.
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation_ Review paper, used for researhc scholars
sap open course for s4hana steps from ECC to s4
cuic standard and advanced reporting.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Approach and Philosophy of On baking technology
Chapter 3 Spatial Domain Image Processing.pdf
Ad

Async in C# - The Good, the Bad and the Ugly