Slide 1
Async in C#, the Old and the New
Slide 2
HELLO!
I am Stuart Lang
You can find me at
stu.dev
DMs open!
Slide 3
Agenda
- Introduction to async and how it works
- A look at the new guidance - do’s & don’ts
- Some new stuff
Slide 4
Slide 5
Slide 6
Slide 7
Slide 8
Safe Abstractions
Dangerous Abstractions Async/Await
“Powerful” Leaky Magic
Slide 9
“It really troubles me how much async, bad async, really bad async we see in the wild” Kathleen Dollard - .NET Rocks! #1143
Slide 10
Slide 11
Asynchrony is viral - ❌ Bad
Slide 12
Asynchrony is viral - ✅ Good
Slide 13
Slide 14
Slide 15
CancellationTokens - ❌ Bad
Slide 16
CancellationTokens - ✅ Good
Slide 17
Slide 18
Slide 19
FlushAsync & Dispose - ❌ Bad
Slide 20
FlushAsync & Dispose - ✅ Good
Slide 21
FlushAsync & Dispose - ✅ Good
Slide 22
FlushAsync & Dispose - ✅ Good
Slide 23
async/await vs return Task- ❌ Bad
Slide 24
async/await vs return Task - ✅ Good
Slide 25
Prefer Task.FromResult over Task.Run - ❌ Bad
Slide 26
Prefer Task.FromResult over Task.Run - ✅ Good
Slide 27
Prefer Task.FromResult over Task.Run - ✅ Good
Slide 28
TaskCompletionSource - ❌ Bad
Slide 29
TaskCompletionSource - ✅ Good
Slide 30
Avoid Task.Run for long blocking - ❌ Bad
Slide 31
Avoid Task.Run for long blocking - ✅ Good
Slide 32
Do not use .Wait() or .Result ❌
Slide 33
Slide 34
Slide 35
Slide 36
Slide 37
Synchronization Context
WindowsFormsSynchronizationContext DispatcherSynchronizationContext Default (ThreadPool) SynchronizationContext AspNetSynchronizationContext stu.dev
Slide 38
Synchronization Context
Specific Thread Used to Execute Delegates
Exclusive (Delegates Execute One at a Time)
WinForms
Yes
Yes
WPF
Yes
Yes
Default
No
No
ASP.NET
No
Yes
Slide 39
Slide 40
Slide 41
Slide 42
Slide 43
Slide 44
Slide 45
Slide 46
Slide 47
Slide 48
Slide 49
Slide 50
Slide 51
Slide 52
Familiar definition
IObservable<T>
IEnumerable<T>
IAsyncEnumerable<T>
Slide 53
Slide 54
Slide 55
THANKS!
Any questions?
You can find me at:
stu.dev