Need to clarify on async-await

xusword

New member
Joined
Jun 2, 2013
Messages
1
Programming Experience
3-5
Hi

I am a java developer trying to catch up in C#. I have been trying to understand async-await keywords and I think I understand most of it but there's just a few things I am not clear about.

C#:
void caller() {

   var valueTask = asyncMethod();
   stuffThatDoNotNeedTaskResult();  // Question B
   var taskResult = await valueTask;
   wrapUp();
}

async Task asyncMethod() {
   var someValue = await someAsyncAPI(); // Question A
   return someValue;
}

Question A: everyone says await do not create to new thread. However, how can the control return to caller when we are still awaiting for someAsyncAPI to come back? How does this work?
Question B: I understand if stuffThatDoNotNeedTaskResult() finishes relatively quickly, the caller would be blocked on the awaitValueTask. What if someAsyncAPI() finishes before stuffThatDoNotNeedTaskResult()? What I have read about so far seems to suggest that some signal would be sent and the execution of stuffThatDoNotNeedTaskResult() would be suspended and the second chunk of asyncMethod() would get the control again and finishes and return the control back to the execution of stuffThatDoNotNeedTaskResult(). This makes sense to me but how and when would stuffThatDoNotNeedTaskResult() be suspended? I am curiously how it works in lower level.
 
Back
Top Bottom