Understanding await control flow [Answered]
According to my understanding, when the control flow reaches an
await
, it would return immediately to the call site. But what would happen if the method you are currently awaiting awaits another method?
Let's say I have 3 methods: Main
, AsyncMethod1
, and AsyncMethod2
. If I call AsyncMethod1
in Main
, the control flow would immediately return to Main
as far as I know. Then, inside AsyncMethod1
, it awaits AsyncMethod2
. In that case, would the control flow return to AsyncMethod1
? But that wouldn't make much sense because AsyncMethod1
is already awaiting something so it cannot continue. Which part of my understanding is wrong?
By the way, if I have an async Main
, where would the control flow return to when I await in Main
? Does it simply not return?19 Replies
You're misunderstanding
await
here. Let's put some real code here to make talking about it easier
When you say await
, you're saying "I would like this method to resume after the Task
that is returned from the method that I'm calling has completed"
And you return a task yourself, so the caller has ability to call you, and then ignore that task for a while, and then eventually check to see if it has completed
But by doing await
, you are signaling "Hey, don't resume me until the thing I awaited is done"
Let's rewrite the above code a bit (the real lowering is much more complicated than this, but this is a good example):Can I understand it as "
await
will return to the call site immediately if and only if the caller did not await
this call"?
No
So does the control flow "return immediately" at all?
Ish
Let's just take
M2
It calls Task.Sleep()
(which returns a task), schedules a continuation after it, and returns that task
Now, to be clear, this isn't precisely how it works. The actual generated code is a good deal more complicatedFrom this example, I understood it as "
await
will only continue after the thing it's awaiting and everything that that thing awaits and so on all completes"Right, that's the effect
All the code knows, though, is that its waiting for that one thing
And however long it takes for those to finish is however long it takes
So if I call an async method in a non-async method, is the only way to retrive the return value to block the thread and wait?
Correct
That's why async is considered "viral"
So when I await something, will the current thread return to the thread pool and be ready to do anything that the OS wants it to do, instead of blocking and do nothing?
Maybe
Depends on whether the thing you await actually defers work and yields the thread
It's possible it completes synchronously
But in the case where it's done multithreadedly, that's what happens right?
Uh... mulithreadedly isn't an adequate description for me to answer
What do you mean? Or should I say asynchronously?
I mean that "mulithreadedly" isn't a description of anything
It's not even a word
Asynchronously is also not a description of anything
Thank you, I got it
!solved
$close
Use the
/close
command to mark a forum thread as answered✅ This post has been marked as answered!