4

I think I'm not understanding something. I had thought that Task.Yield() forced a new thread/context to be started for a task but upon re-reading this answer it seems that it merely forces the method to be async. It will still be on the same context.

What's the correct way - in an asp.net process - to create and run multiple tasks in parallel without causing deadlock?

In other words, suppose I have the following method:

async Task createFileFromLongRunningComputation(int input) { 
    //many levels of async code
}

And when a certain POST route is hit, I want to simultaneously launch the above methods 3 times, return immediately, but log when all three are done.

I think I need to put something like this into my action

public IHttpAction Post() {
   Task.WhenAll(
       createFileFromLongRunningComputation(1),
       createFileFromLongRunningComputation(2),
       createFileFromLongRunningComputation(3)
   ).ContinueWith((Task t) =>
      logger.Log("Computation completed")
   ).ConfigureAwait(false);
   return Ok();

}

What needs to go into createFileFromLongRunningComputation? I had thought Task.Yield was correct but it apparently is not.


  • Calling ConfigureAwait is pointless if you never actually await the Task. - Servy
  • Thanks @Servy I kind of knew that but it slipped my mind - George Mauer

Linked


Related

Latest