我有一个程序可以访问通过 HTTP 运行的 WCF Web 服务。
程序需要登录,登录过程是这样的:
Get Staff List from WebService.
Gets Drivers List from Webservice.
Gets Vehcile list from web service.
logged in.
我想在每个阶段之后更新进度。所以我想我会通过 IAsycnhResult 方法链接它们..
但是,当我尝试从这些句柄回调方法更新进度时,我收到一个错误:
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;
}
}
您如何安全地更新进度?