0
我想等到異步WCF服務操作完成。怎麼做 ?WCF異步方法:等到操作完成
Service.WebService.GetUserCompleted += new EventHandler<BGWebService.GetUserCompletedEventArgs>(WebService_GetUserCompleted);
Service.WebService.GetUserAsync(UserId);
我想等到異步WCF服務操作完成。怎麼做 ?WCF異步方法:等到操作完成
Service.WebService.GetUserCompleted += new EventHandler<BGWebService.GetUserCompletedEventArgs>(WebService_GetUserCompleted);
Service.WebService.GetUserAsync(UserId);
GetUserAsync
應返回IAsyncResult
(see MSDN documentation)的生成的方法。您可以使用它來阻止,直到操作完成:
IAsyncResult asyncResult = Service.WebService.GetUserAsync(UserId);
asyncResult.AsyncWaitHandle.WaitOne();
那麼我試着以不同的方式做到這一點。我沒有等待服務響應,而是打電話給我正在處理的界面。
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();
}
我真的很抱歉。我也從某個源複製了代碼,但是我錯誤地鏈接了該鏈接。
我已經嘗試過了,但是我無法將GetUserAsync從無效類型轉換爲IAsyncResult – Tony 2011-06-07 18:52:35
您使用的是爲Silverlight生成的代理嗎? – 2011-06-07 19:14:23