I am rewriting some of my component management to use async start methods. Sadly it looks like a call to an async method WITHOUT await, still does await the result?
Can anyone enlighten me?
I am calling:
public async Task StartAsync() {
await DoStartProcessingAsync();
}
which in itself is calling a slow implementation of protected abstract Task DoStartProcessingAsync(); - slow because it dome some EF calls, then creates an appdomain etc. - takes "ages".
The actual call is done in the form:
x.StartAsync().Forget();
with "Forget" being a dummy function just to avoid the "no await" warning:
public static void Forget(this Task task) {
}
Sadly, this sequence - is waiting for the slow DoStartAsync method to complete, and I see no reason for that. I am quite old in C#, but quite new to async/await and I was under the impression that, unless I await for the async task - the method would complete. As such, I expected the call to StartAsyc().Forget() to return immediatly. INSTEAD stack trace shows the thread going all the way into the DoStartProcessingAsync() method without any async processing happening.
Anyone can enlighten me on my mistake?
DoStartProcessingAsync()? Just to make sure it's not only calledAsyncbut really is awaiting something and not working synchronously.async Task Blah() { await Something(); }should be just the same as justSomething()- this does not magically make it happen in the backgroundasyncmethods will run synchronously till it reaches first await. If there is no await, it runs completely synchronously. Can you post the code ofDoStartProcessingAsync?StartAsyncasync and insideawaitingDoStartProcessingAsync(). Why don't you just remove those particular keywords then dovar result = x.StartAsync()and it should just workasynckeyword doesn't automagically makes a method asynchronous. There needs to be aawaitkeyword and the method you're awaiting should also be asynchronous. If you have time consuming parts in the method, then consider calling it in another thread i.eawait Task.Run(()=> DoStartProcessingAsync());