[英]Updating Progress when Handling Call backs with WCF
I have a program that accesses a WCF Web Service I have running via HTTP. 我有一个程序可以访问通过HTTP运行的WCF Web服务。
The program needs to login and the login process is this: 该程序需要登录,登录过程如下:
Get Staff List from WebService.
Gets Drivers List from Webservice.
Gets Vehcile list from web service.
logged in.
I would like to update the progress after each stage. 我想在每个阶段之后更新进度。 So I thought I would chain them via the IAsycnhResult methods..
所以我想我可以通过IAsycnhResult方法链接它们。
However when I try and update the progress from these handlecallback methods, i get an error: 但是,当我尝试通过这些handlecallback方法更新进度时,出现错误:
The calling thread cannot access this object because a different thread owns it.
Code: 码:
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;
}
}
How do you update the progress safely? 您如何安全地更新进度?
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.