I have a question about Webapi2
My application is fully async/await
, but I want to optimize the last part. I have a hard time finding out, so is there a
The code after the return
statement will not be executed in your second example, but these tasks can be offloaded to the ThreadPool
:
public async Task Foo(Bar bar)
{
List tasks = new List();
var actualresult = Barfoo(bar.Bar);
foreach(var foobar in bar.Foo)
{
//some stuff which fills tasks for extra logic, not important for the client
Task.Run(() => /* foobar task creation, queued on worker threads */);
}
// this will execute without waiting for the foobar logic to finish
return Ok(actualresult.Result);
}
If you want to later check the 'extra logic' tasks for completion or errors, you may want to look into the Task Parallel Library