Application
I have a View Model referenced by several projects or Views .
Due to the API, some of the View projects are asynchronous, while others are not.
The View project implements an interface implementation in the View Model using the IOC container.
The code
Consider this minimal code example.
Interface:
public interface IDatabase { Task<Data> GetData(); }
View Model expects the GetData method:
pubic class DisplayDataViewModel { private readonly IDatabase _database public DisplayDataViewModel(IDatabase database) { _database = database; } private async Task GetDataFromDatabase() { Data data = await _database.GetData();
Asynchronous View The project uses the Async function to receive data.
public class AsynchronousViewDataBase : IDatabase { public async Task<Data> GetData() { return await GetDataAsync(); } }
Synchronous View The project uses a synchronous call to retrieve data, but due to interface limitations I need to return the task, but I'm just interested in returning Data , which results in the async method.
public class SynchronousViewDatabase : IDatabase { public async Task<Data> GetData() { return GetData(); } }
But , then he gives a warning:
In this asynchronous method, there are no “wait” statements and will be executed synchronously. Think about how to use the “wait” operator to wait for non-blocking API calls or “wait for Task.Run (...)” to do the work of binding to the processor in the background thread.
What doesn’t matter to me, since I don’t care if it works synchronously.
Using Task.Run to suppress a warning seems unnecessary because the call (and in my case) comes from a background thread.
Question
Is there any best practice for implementing this type of non-expecting asynchronous method? Can a warning be ignored? Is there a better way to implement async synchronous call?
Additional
I converted the interface to a synchronous call and used Task.Result in Asynchronous View (which fixed the warning), but this caused the application to stop responding and there were just a lot of difficult problems.
Note. I am not stating that using the async magically causes the method to run asynchronously.