The <TResult> .ConfigureAwait (false) task resumes in the captured context
I have a block of code:
Task task2Seconds = Wait2Seconds();
Task task5Seconds = Wait5Seconds();
await task5Seconds;
await task2Seconds.ConfigureAwait(false);
- The first task is expected to resume in the same (captured) context, but it takes 5 seconds.
- A second task is expected, which does not resume in the captured context and only takes 2 seconds.
- The result after the second task is waiting, it still resumes in the captured context.
I don't understand the basic logic, can someone please explain to me?
Thank you Ha
source to share
When you call ConfigureAwait(false)
, you are telling .NET that when you resume execution after, the await
framework is not required to resume execution in the same context. But this is not the same as telling the framework that it should not resume in the same context.
Your second one is await
waiting for a task that has already been completed. In this way, continuation can be performed immediately and synchronously, that is, without giving up flow control. This way your code remains in the same context in this case.
Not that it await
specifically brings you back to your original context. Rather, it's just that he had no reason to leave that context, and so you're still there when it finishes await
.
source to share