I have a Base class which provides a method with the following signature:
virtual async Task RunAsync()
Derived classes should override this implementation with something like
public override async Task RunAsync() { await base.RunAsync(); await this.DoSomethingElse(); }
Now the compiler gives me a warning that the Base.RunAsync is lacking an await statement. I want that my code is warning free, not suppressing warnings and that it runs without having to provide null checks even if the derived class doesn't provide an override implementation, therefore going with virtual Task RunAsync()
(omitting the async keyword) is not possible.
For now the implementation in the base method is await Task.Delay(0);
which dismisses the compiler warning, yet is somehow not "clean" in my opinion. What is the right way to cope with this subject?
When the await operator is applied to the operand that represents an already completed operation, it returns the result of the operation immediately without suspension of the enclosing method. The await operator doesn't block the thread that evaluates the async method.
The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete. await can only be used inside an async method.
Async methods call in a function that does not support concurrency. Using async methods in Swift might result in an error like “'async' call in a function that does not support concurrency.” Using the async method using the trailing closure, we create an environment in which we can call asynchronous methods.
When we don't want to return a result from our async method, we should always return a Task. To validate our asynchronous operations, we have to use the await keyword while calling that operation. When we convert our synchronous code to asynchronous, we have to use the async and await keywords all the way up the chain.
You misunderstand how async
works. It's not part of the signature, so if you don't need the await
, you don't actually have to use it. So even though you are providing a Task RunAsync()
method, you can just write it without the async
, for example:
virtual Task RunAsync() { return Task.FromResult(false); }
This still allows you to make an override that does in fact use await
, like so:
override async Task RunAsync() { await base.RunAsync(); await DoSomeOtherStuffAsync(); }
This is the reason why the compiler gives you the warning - the async
keyword is completely useless, and not part of the method signature. It's just a signal to the compiler that it's allowed to use the identifier await
as a keyword, basically - to maintain backwards compatibility with older C# code.
EDIT:
As noted by Jeroen Mostert in the comments, from .NET Framework 4.6 on, the Task.CompletedTask
property is public, so you can do this:
virtual Task RunAsync() { return Task.CompletedTask; }
So if you can use 4.6+, this is the newest, cleanest option. And of course, if you need the task to actually return a value, Task.FromResult
is still there :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With