...

/

Caveats

Caveats

This lesson explains that asynchronous invocation don't create any new threads and the concept of synchronization context.

Caveats

In this lesson, we’ll address a common confusion when folks start working with the asynchronous programming paradigm. The incorrect belief is that somehow, somewhere there’s another thread that is doing the work when we await an operation. Consider the MyAwaitableType type we worked on in the previous sections. When we execute the following statement, we have another thread that gets blocked on Thread.Sleep() in our implementation.

Press + to interact
MyAwaitableType awaitable = new MyAwaitableType(1000)
await awaitable

Behind the scenes, we sleep a thread and invoke the continuation when the sleep period finishes. Does this mean we are simply shifting the burden from one thread to another? We are not blocking the thread that executes the await expression but blocking another on the Thread.Sleep() invocation. Indeed our implementation does that and only exists for demonstration purposes. In reality, the entire premise of async programming is to not block threads. In fact, when we invoke Task.Delay() there is no thread that gets blocked or is waiting for the ...

Access this course and 1400+ top-rated courses and projects.