I have an async method:
public async Task DoSomethingAsync(){
...
await ...
await ...
....
return await SaveAsync();
}
In most time, I call this method by:
await DoSomethingAsync()
This call works as expected. But somewhere, I need to call this method as fire and forget:
public void OtherMethod()
{
...
DoSomethingAsync(); //fire and forget here
}
In this case, sometimes the Task DoSomethingAsync()
runs and completes, but sometimes the task never invoked (or invoke some await
s within DoSomethingAsync()
but never complete the last await SaveAsync();
).
I'm trying to make sure the task will be invoked in fire and forget manner by these code:
public void OtherMethod()
{
...
Task.Factory.StartNew(() =>
{
await DoSomethingAsync();
}); //fire and forget again here
}
However, this does not work as expectation. So my questions are:
How to make the call to
DoSomethingAsync()
withoutawait
will always run and complete? (I don't care the case AppDomain restart/crash)If I remove all
async/await
code withinDoSomethingAsync()
and replaceawait
by.ContinueWith()
, then the call toTask DoSomethingAsync()
(not haveasync
in method declaration) will be invoked and sure to complete (ignore case AppDomain restart/crash), if yes, how long from the call (I don't think that I'll be happy if the Task will be invoked after 10 minutes)?