Async in C#, the Old and the New

A presentation at DDD North 2020 in February 2020 in Hull, UK by Stuart Lang

Slide 1

Slide 1

Async in C#, the Old and the New

Slide 2

Slide 2

HELLO!

I am Stuart Lang You can find me at stu.dev DMs open!

Slide 3

Slide 3

Agenda

  • Introduction to async and how it works
  • A look at the new guidance - do’s & don’ts
  • Some new stuff
    • Async Streams
    • Channels

Slide 4

Slide 4

Why async/await

Slide 5

Slide 5

How does it work?

Slide 6

Slide 6

How does it work?

Compiler generated code

Slide 7

Slide 7

How does it work?

Slide 8

Slide 8

Safe Abstractions Dangerous Abstractions Async/Await “Powerful” Leaky Magic

Slide 9

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

Slide 11

Asynchrony is viral - ❌ Bad

Slide 12

Slide 12

Asynchrony is viral - ✅ Good

Slide 13

Slide 13

Async void - ❌ Bad

Slide 14

Slide 14

Async void - ✅ Good

Slide 15

Slide 15

CancellationTokens - ❌ Bad

Slide 16

Slide 16

CancellationTokens - ✅ Good

Slide 17

Slide 17

Using a timeout - ❌ Bad

Slide 18

Slide 18

Using a timeout - ✅ Good

Slide 19

Slide 19

FlushAsync & Dispose - ❌ Bad

Slide 20

Slide 20

FlushAsync & Dispose - ✅ Good

Slide 21

Slide 21

FlushAsync & Dispose - ✅ Good

Slide 22

Slide 22

FlushAsync & Dispose - ✅ Good

Slide 23

Slide 23

async/await vs return Task- ❌ Bad

Slide 24

Slide 24

async/await vs return Task - ✅ Good

Slide 25

Slide 25

Prefer Task.FromResult over Task.Run - ❌ Bad

Slide 26

Slide 26

Prefer Task.FromResult over Task.Run - ✅ Good

Slide 27

Slide 27

Prefer Task.FromResult over Task.Run - ✅ Good

Slide 28

Slide 28

TaskCompletionSource - ❌ Bad

Slide 29

Slide 29

TaskCompletionSource - ✅ Good

Slide 30

Slide 30

Avoid Task.Run for long blocking - ❌ Bad

Slide 31

Slide 31

Avoid Task.Run for long blocking - ✅ Good

Slide 32

Slide 32

Do not use .Wait() or .Result ❌

Slide 33

Slide 33

SynchronizationContext?

Slide 34

Slide 34

How does it work?

Slide 35

Slide 35

How does it work?

Slide 36

Slide 36

Synchronization Context

Slide 37

Slide 37

Synchronization Context

WindowsFormsSynchronizationContext DispatcherSynchronizationContext Default (ThreadPool) SynchronizationContext AspNetSynchronizationContext stu.dev

Slide 38

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 39

Slide 40

Slide 40

KA-BLAMO!

Slide 41

Slide 41

Slide 42

Slide 42

Slide 43

Slide 43

KA-BLAMO!

Slide 44

Slide 44

Slide 45

Slide 45

Slide 46

Slide 47

Slide 48

Slide 49

Slide 49

IAsyncEnumerable<T>

Slide 50

Slide 50

The problem

Slide 51

Slide 51

Familiar definition

Slide 52

Slide 52

Familiar definition

IObservable<T> IEnumerable<T> IAsyncEnumerable<T>

Slide 53

Slide 53

Slide 54

Slide 54

Demo - IAsyncEnumerable<T> & Channels

Watch the demo here!

Slide 55

Slide 55

THANKS!

Any questions? You can find me at: stu.dev