我有一个异步运行的 Windows svc(我编辑了方法及其参数以使其异步),有点像:http: //msdn.microsoft.com/en-us/library/ms731177.aspx
但是,我调用了我想要异步运行的任务(对服务/服务器的调用),然后更新 UI(在后台工作程序上使用 ReportProgress() - 所有这些都发生在后台工作程序的 dowork() 方法中)。但是,我调用 Endxxx 方法得到结果,但问题是,我的代码不应该是这样的吗?
while (!asyncresult.IsCompleted) { // Do all UI updating etc here... }
// Call endXXX here.
但是,这种方法会锁定 UI。目前,我的代码是这样的(并且不锁定 UI):
IAsyncResult res = null;
try
{
res = serviceX.BeginXXX(ResultCallBack, "");
}
catch (FaultException<ManagementException> managementEx)
{
Logger.Error(managementEx.Detail.ToString());
MessageBox.Show("Could not add printer. See log.");
}
InstallBackgoundWorker.ReportProgress(90);
InstallBackgoundWorker.ReportProgress(91);
InstallBackgoundWorker.ReportProgress(93);
InstallBackgoundWorker.ReportProgress(94);
InstallBackgoundWorker.ReportProgress(95);
InstallBackgoundWorker.ReportProgress(96);
InstallBackgoundWorker.ReportProgress(97);
if (res.IsCompleted)
{
ResultCallBack(res);
}
InstallBackgoundWorker.ReportProgress(100);
它是否正确?这对我来说似乎是错误的。