Similar to Implementing an interface that requires a Task return type in synchronous code although I\'m curious if I should just ignore the compiler error my situation gener
What should determine when I choose to ignore this compiler warning? In some cases the work is so simple that spawning a thread for it is undeniably counter-productive.
The compiler isn't saying "use Task.Run inside this method". It is merely telling you that you prepared him for an async method, adding the async modifier to your method declaration, but you aren't actually awaiting anything.
You could take three approaches:
A. You could ignore the compiler warning and everything will still execute. Note, that there will be a slight overhead of state-machine generation, but your method call will execute synchronously. If the operation is time consuming and might cause the method execution to make a blocking call, this might confuse the users consuming this method.
B. Separate the generation of the "Awesome" into a synchronous interface and an asynchronous interface:
public interface MakeAwesomeAsync
{
Task MakeAwesomeAsync();
}
public interface MakeAwesome
{
void MakeAwesome();
}
C. If the operation is not too time consuming, you can simply wrap it in a Task using Task.FromResult. I would definitely measure how long it takes to run the CPU bound operation before choosing this.