The problem is that async/await
is about asynchrony, not threads.
If you use Task.Run
, it will indeed use a background thread (via the Thread Pool, via the Task Parallel Library).
However, for IO operations it relies on IO Completion ports to notify when the operation is complete.
The only guarantee async/await
makes is that when an operation completes, it will return to your caller in the SynchronizationContext that was there when it began. In practical terms, that means it will return on the UI Thread (in a Windows application) or to a thread that can return the HTTP Response (in ASP.NET)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…