问题描述
我想了解异步的使用等待和我学的几篇博客,现在我已经做了一个测试code,但它不工作的方式,我希望它的工作。
我有它返回列表的方法:
私人列表<员工>装getEmployees()
{
IList的<员工>名单=新的名单,其中,员工>();
list.Add(新员工(){n = 1,年龄= 20,名称=Kavin});
list.Add(新员工(){n = 2,年龄= 30,名称=阿伦});
list.Add(新员工(){n = 3,年龄= 20,名称=苏雷什});
list.Add(新员工(){n = 4,年龄= 30,名称=周杰伦});
list.Add(新员工(){n = 5,年龄= 20,名称=南大});
list.Add(新员工(){n = 5,年龄= 20,名称=Kavin});
list.Add(新员工(){n = 5,年龄= 20,名称=Kavin});
list.Add(新员工(){n = 1,年龄= 23,名称=测试});
返回列表;
}
然后,我写我的异步方法:
专用异步任务<列表<员工>> TestEmployeesGetAsync()
{
变种结果=等待Task.Run(()=>装getEmployees());
返回结果;
}
当我把这种方式:
VAR的结果= TestEmployeesGetAsync();
在Visual Studio告诉我它返回任务<列表< T>>
,它的用法是:
名单,其中,员工>结果=等待TestEmployeesGetAsync();
为什么我需要把计谋的调用方法,如果我把计谋
它给人当然编译器错误,因为等待
应该有异步为好。有人可以清楚我的心里是怎么称呼它,这样我就可以得到名单,其中,T>
而不是任务<列表< T>>
有一些依赖编译器的需要的,以便了解你正在运行的异步方法。该信号在方法声明中的异步
修改。一旦你将其标记为异步
,您可以使用计谋
关键字。这就是为什么异步传播一路向下调用堆栈,当你的一个电话异步方法,需要等待它的结果,你需要标记的异步修改的消费方式。
为了让您的方式工作,你需要做到以下几点:
公共异步任务GetEmployeesAsync()
{
名单<员工>结果=等待TestEmployeesGetAsync();
}
作为一个侧面说明,要知道, 你不应该暴露异步包装过的同步方法的。
I am trying to understand the usage of async await and i studied few blog posts and now i have made a testing code but it is not working the way i am expecting it to work.
I have a method which returns 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 async method:
private async Task<List<Employee>> TestEmployeesGetAsync()
{
var result = await Task.Run(() => GetEmployees());
return result;
}
When i call this method :
var Result = TestEmployeesGetAsync();
The visual studio is telling me that it returns Task<List<T>>
and it usage is:
List<Employee> result = await TestEmployeesGetAsync();
Why i need to put await on the calling method if i put await
it gives compiler error of course because await
should have async as well. Can somebody clear my mind how to call it so that i can get List<T>
instead of Task<List<T>>
There are a few dependencies the compiler needs in order to understand you're running an async method. The signal is the async
modifier on the method declaration. Once you mark it as async
, you can use the await
keyword. That is why async propagates "all the way" down the call stack, as when you call one async method and need to await its result, you'll need to mark the consuming method with the async modifier.
In order to make your method work, you'll need to do the following:
public async Task GetEmployeesAsync()
{
List<Employees> result = await TestEmployeesGetAsync();
}
As a side note, be aware that you should not expose async wrappers over sync methods.
这篇关于异步的await返回任务&LT;列表&LT; T&GT;&GT;而不是List&LT; T&GT;在调用aync方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!