Let's suppose i have the following async method that needs fairly long time until completing its work:
void async Task LongWork()
{
await LONGWORK() // ... long work
}
Now, in an web api, i would like to run that work in a background (i.e., i want to return the Http Request after starting that LongWork() but before its completion:
I can think of three approaches to achieve this:
1) public async Task<string> WebApi()
{
... // do another work
await Task.Factory.StartNew(() => LongWork());
return "ok";
}
2) public async Task<string> WebApi()
{
... // do another work
await Task.Factory.StartNew(async () => await LongWork());
return "ok";
}
3) public async Task<string> WebApi()
{
... // do another work
Task.Factory.StartNew(async () => await LongWork());
return "ok";
}
Q1: What's the difference between approach #1 and #2?
Q2: What is the right way to, in the ASP.NET world, run a method (in this example, LongWork() containing some async/await pairs in a background thread? In particular, in #3, there's no "await" before Task.Factory.StartNew(async () => await LongWork()). Is it fine?
Thanks!