我想等到异步WCF服务操作完成。怎么做?
Service.WebService.GetUserCompleted += new EventHandler<BGWebService.GetUserCompletedEventArgs>(WebService_GetUserCompleted);
Service.WebService.GetUserAsync(UserId);
答案 0 :(得分:0)
生成的方法GetUserAsync
应返回IAsyncResult
(see MSDN documentation)。您可以使用它来阻止操作完成:
IAsyncResult asyncResult = Service.WebService.GetUserAsync(UserId);
asyncResult.AsyncWaitHandle.WaitOne();
答案 1 :(得分:0)
我尝试以不同的方式做到这一点。我没有等待服务响应,而是发送了一个调用我正在处理的接口。
public class Person : INotifyPropertyChanged
{
//Calling the service to get the object needed
private string personname
public string PersonName
{
get {
return personname;
}
set {
personname = value;
NotifyPropertyChanged("PersonName");
}
}
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void LoadMembersName()
{
ServiceSoapClient serviceClient = new ServiceSoapClient() {};
serviceClient.GetMembersNameCompleted += (sender, e)=>{
PersonName = e.Result
} ;
}
}
现在在界面上我们只需要加载人名并在触发属性更改事件后执行操作。
void Page_Load (object sender, EventArgs e)
{
Person per = new Person();
per.PropertyChanged += (sender,e) =>
{
//Your code here
Response.Write(per.PersonName);
};
per.LoadMembersName();
}
我真的很抱歉。我也从某些来源复制了代码,但我错放了链接。