Question

I am using async action methods:

public async Task<JsonResult> SetMaterialRequisitionNoteStatus(List<Guid> ids, string statusName, Guid statusId)
{
   string resp = string.Empty;
   resp = await note.GeneratePartLifeNotifications_ORN(repairNote, "Outsourced Repair Note " + statusName);
   if (responce.ToLower() != "ok")
   {
      responce = responce.Replace("Workorder", "Outsource repair note");
      sendNotification(responce);
   }

   Utilities.SaveEntityInstance(note);
  ...... 
}

But my code is not working asynchronously. The line next to await calls wait until we get the result from GeneratePartLifeNotifications_ORN method. It is working as same synchronous code. I want asynchronous behaviour.

Was it helpful?

Solution

The line next to await calls wait until we get the result from generatePartLifeNotifications_ORN method.

Well, what did you expect? await will execute that method asynchronously and will release the current thread for doing work somewhere else. When that asynchronous task is completed, the thread will comeback to pick the function execution from there with the result (unless you specify .ConfigureAwait(false), then any thread could pick the execution).

"Await" means something like "wait for", so the execution is waiting for that method to complete, but the thread left to work in something else and it is not waiting.

If you executed this in a synchronous fashion, the execution order will be the same, but the calling thread will be blocked waiting for GeneratePartLifeNotifications_ORN to finish. The magic of async, is to allow threads to working in something else instead of waiting for things that are happening away (like a webservice, or a database).

Cheers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top