![](/img/trans.png)
[英]Why to use call backs when same thing can be done just by calling the method
[英]Updating Progress when Handling Call backs with WCF
我有一個程序可以訪問通過HTTP運行的WCF Web服務。
該程序需要登錄,登錄過程如下:
Get Staff List from WebService.
Gets Drivers List from Webservice.
Gets Vehcile list from web service.
logged in.
我想在每個階段之后更新進度。 所以我想我可以通過IAsycnhResult方法鏈接它們。
但是,當我嘗試通過這些handlecallback方法更新進度時,出現錯誤:
The calling thread cannot access this object because a different thread owns it.
碼:
ASreference.Service1Client client = new ASreference.Service1Client();
private void login()
{
//User has already entered username/password
AsyncCallback CallBack = new AsyncCallback(HandleCallback);
client.BeginGetStaff(CallBack, client);
TxtboxPassword.IsEnabled = false;
TxtboxUsername.IsEnabled = false;
LblProgress.Content = "Logging In";
progress.Value = 10;
}
void HandleCallback(IAsyncResult result)
{
bool success = false;
try
{
Staff = client.EndGetStaff(result);
success = true;
}
catch
{
MessageBox.Show("Incorrect username or password");
client = new ASreference.Service1Client();
EnableTextbox();
}
if (success)
{
AsyncCallback CallBack = new AsyncCallback(DriversCallBack);
client.BeginGetDrivers(CallBack, client);
LblProgress.Content = "Downloading Drivers"; //CAUSES ERROR
progress.Value = 30;
}
}
您如何安全地更新進度?
progress.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
LblProgress.Content = "Downloading Drivers";
progress.Value = 30;
}
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.