When await is encountered, control goes back to caller, while the awaited task runs (makes/waits for network request/response) in background. I know that awaiting task will not need thread when it is awaiting for network response as it is not really running anything but just waiting.
I want to ask - suppose in the awaited function, there is some synchronous code like Console.WriteLine, then because control has gone back to the caller, who executes the console.writeline?
Assume button click event does await Fn1();.
Example 1 :
async Task<string> fn1() {
string result = await NetworkCallAsync('http......');
return result;
}
In above example upon encountering await Fn1(), control goes back to the UI thread, while the network call is made. I understand this. When network call completes, the Ui thread will execute the rest of the code. That is - fetch the result and return.
Example 2:
async Task<string> fn1() {
Console.WriteLine("hello"); //or any other synchronous code like Thread.Sleep(10);
string result = await NetworkCallAsync('http......');
return result;
}
In above code, when the button click encounters the await Fn1() and has returned control to the UI thread, then who executes the Console.WriteLine (or Thread.Sleep(10)) as shown in above example)?