[英]Wait return from async task method
我是代码的初学者,已经花费了数小时来寻找问题的解决方案。
我在一个可与其他Web服务一起使用的Silverlight应用程序中,并用异步方法调用它们(我没有选择的原因,因为我正在使用Silverlight),目标是在继续执行该应用程序之前就太等待方法的返回。
这就是我的方法的外观(它们在名为Services
的静态类中):
private static Task<ObservableCollection<Demande_GetNb_Result>> DemandeGetNbAsync()
{
TaskCompletionSource<ObservableCollection<Demande_GetNb_Result>> tcs = new TaskCompletionSource<ObservableCollection<Demande_GetNb_Result>>();
ABClient aBClient = new ABClient();
aBClient.Demande_GetNbCompleted += (sender, e) =>
{
if (e.Error != null)
tcs.TrySetException(e.Error);
else if (e.Cancelled)
tcs.TrySetCanceled();
else
tcs.TrySetResult(e.Result);
};
aBClient.Demande_GetNbAsync(((App)Application.Current).User.IdUser, true, true, true, (int)Constantes.Statut.Is_Waiting);
return tcs.Task;
}
public static async Task StartFamille()
{
ListInfos = await DemandeGetNbAsync(); // ListInfos is a Static variable on my Services class
}
我在其他这样的类中调用此方法:
var result = Services.StartFamille();
我想等到结果有价值之后再继续执行,但是它不起作用,而且我找不到我能理解的简单解决方案。 继续执行,而无需等待将值分配给结果。
您的方法返回Task
。 尝试添加等待操作符。
var result = await Services.StartFamille();
此外,DemandeGetNbAsync方法应返回ObservableCollection<Demande_GetNb_Result>
类型的变量,并且还应使用await运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.