Async expects Task <List <T>> to return instead of List <T> when calling the aync method
I am trying to understand the use of async waiting, and I studied several blog posts, and now I have done some test code, but it does not work the way I expect it to work.
I have a method that returns a List:
private List<Employee> GetEmployees() { IList<Employee> list = new List<Employee>(); list.Add(new Employee() { Id = 1, Age = 20, Name = "Kavin" }); list.Add(new Employee() { Id = 2, Age = 30, Name = "Alen" }); list.Add(new Employee() { Id = 3, Age = 20, Name = "Suresh" }); list.Add(new Employee() { Id = 4, Age = 30, Name = "Jay" }); list.Add(new Employee() { Id = 5, Age = 20, Name = "Nanda" }); list.Add(new Employee() { Id = 5, Age = 20, Name = "Kavin" }); list.Add(new Employee() { Id = 5, Age = 20, Name = "Kavin" }); list.Add(new Employee() { Id = 1, Age = 23, Name = "Test" }); return list; } Then I wrote my asynchronous method:
private async Task<List<Employee>> TestEmployeesGetAsync() { var result = await Task.Run(() => GetEmployees()); return result; } When I call this method:
var Result = TestEmployeesGetAsync(); Visual Studio tells me that it returns Task<List<T>> , and its use:
List<Employee> result = await TestEmployeesGetAsync(); Why do I need to put the wait on the calling method, if I put await , it gives a compiler error, because await must have asynchronous mode. Can someone clear my mind what to call it so that I can get List<T> instead of Task<List<T>>
Why do I need to put a pending call to the calling method if I put it on hold? a compiler error, of course, because the wait should also be asynchronous.
There are several dependencies needed by the compiler to understand that you are using the async method. A signal is an async modifier in a method declaration. Once you mark it as async , you can use the await keyword. This is why async propagates all the way down the call stack, since when you call one asynchronization method and you need to wait for its result, you will need to mark the consumption method with the async modifier.
For your method to work, you need to do the following:
public async Task GetEmployeesAsync() { List<Employees> result = await TestEmployeesGetAsync(); } As a note, note that you should not expose asynchronous wrappers using synchronization methods .
You need to wait for the result of the task returned by TestEmployeesGetAsync . You can do this asynchronously with await , which expands the result for you to List<Employee> , or you can get the result from the task using the Result property. However, this can cause a dead end , so you need to be careful.
With async-await it strives to break through the chain of calls, that is, waiting for the async method, you need to do this in another async method (as you found out), and waiting for this method you need to be another async method, and therefore async methods are propagated through the code base until you reach the event handler or Main (which cannot be marked async ).
This proliferation of async methods is not unusual, and to avoid this, you can wait for the task to complete using Task.Wait and Task.Result , but it is a blocking method and can cause the aforementioned deadlock. It is also an anti-pattern called sync over async, and it defeats the goal of doing the work asynchronously, as you end up blocking it to the end.
As pointed out by @BenVoigt, one exception is that you run multiple asynchronous requests and then block waiting for all tasks to complete using Task.WaitAll .