Need to clarify on async-await


New member
Jun 2, 2013
Programming Experience

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.

void caller() {

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

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.
Top Bottom