.NET 4.5异步等待和重载方法 [英] .NET 4.5 async await and overloaded methods
问题描述
我有一个异步方法:
public async Task<UserLoginExResult> LoginExAsync(CustomTable exRequest, string language, bool throwEx = true)
{
UserLoginExResult result = await UserService.LoginExAsync(UserGroup, language, TimezoneOffset, GetDeviceInfo(), GetLoginProperties(), exRequest);
ProcessLoginResult(result, false, throwEx);
return result;
}
和过载:
public Task<UserLoginExResult> LoginExAsync(CustomTable exRequest, bool throwEx = true)
{
return LoginExAsync(exRequest, Language.ID, throwEx);
}
我不知道我是否应该标注超载一(一个具有较少参数)为异步
和使用计谋
?我想我不应该,但你能告诉我什么发生对子级,如果我这样做吗?我很迷失在这里,而不是真的知道什么工作
它会等待?它会创建一个额外的工作
或等待
不创建一个新的工作
?
I'm not sure if I should mark the overloaded one (the one with fewer parameters) as async
and use await
? I guess I should not but can you tell me what whould happen if I would do that? I'm quite lost in here and not really sure what Task
it would wait for? Would it create an extra Task
or await
doesn't create a new Task
?
推荐答案
没有,有一个在使用异步方法时,它是没有什么好处的只是的去包装和解开一个已经存在的。它的优良只是有一个正常的方法,在这里,委托给真正的异步方法。
No, there's little benefit in using an async method when it's just going to wrap and unwrap an existing one. It's fine to just have a "normal" method here, delegating to the "real" async method.
(他们不是的非常的相同 - 例如,如果 UserService.LoginExAsync
方法抛出一个异常,而不是返回一个失败的任务,异步/计谋版本将只返回一个失败的任务,而其他版本的也的立即抛出。)
(They're not quite the same - for example, if the UserService.LoginExAsync
method throws an exception rather than returning a failed task, the async/await version will just return a failed task, whereas the other version will also throw immediately.)
这篇关于.NET 4.5异步等待和重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!