Async Await will only improve performance if it allows the machine to do additional work
suggest changeConsider the following code:
public async Task MethodA()
{
await MethodB();
// Do other work
}
public async Task MethodB()
{
await MethodC();
// Do other work
}
public async Task MethodC()
{
// Or await some other async work
await Task.Delay(100);
}
This will not perform any better than
public void MethodA()
{
MethodB();
// Do other work
}
public void MethodB()
{
MethodC();
// Do other work
}
public void MethodC()
{
Thread.Sleep(100);
}
The primary purpose of async/await is to let the machine do other work — for example, to let the calling thread run other requests while it waits for I/O. In this chain, nothing overlaps the delay: each method awaits the next one before its own “other work,” so the latency is no better than calling MethodA, MethodB, and MethodC synchronously. Task.Delay still releases the thread, unlike Thread.Sleep, which can matter for a server even when this call chain itself is no faster.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents