IAsyncresult - 轮询而不冻结 UI?
我有一个异步运行的 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);
这是正确的吗?对我来说这似乎是错误的。
I've got a windows svc which is running asynchronously (I've edited the methods and their parameters to make them async), a little like: http://msdn.microsoft.com/en-us/library/ms731177.aspx
However, I call the task which I want to run asynchronously (the call to the service/server), and then update the UI (using ReportProgress() on the backgroundworker - all of this is happening in the dowork() method of a backgroundworker). However, I call the Endxxx method to get the results, but the problem is, shouldn't my code look like?
while (!asyncresult.IsCompleted) { // Do all UI updating etc here... }
// Call endXXX here.
However, this approach locks the UI. At the moment, my code is like so (and doesn't lock the 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);
Is this correct? It seems wrong to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您是否正确使用了异步模式。它应该看起来像这样:
用
Stream
编写,因为我不知道您的对象的签名,但希望您明白!您需要在给定的回调中处理操作的完成,而不是在调用 Begin 方法之后直接处理。您不需要轮询IsCompleted
属性。I'm not certain that you're using the async pattern correctly. It should look something like this:
Written with
Stream
because I don't know the signature of your object, but hopefully you get the idea! You need to process the completion of the operation in the callback you give it, not directly after you call the Begin method. You shouldn't need to poll theIsCompleted
property.不,您不应该采用第一种方法,因为它违背了以异步方式调用方法的目标。
第二种方法也很麻烦,因为
除非异步只提供它,否则无法显示异步作业的进度报告。
解决方案是:
您还必须注意从后台线程以及在 Windows 窗体中使用 Invoke 以及在 WPF 中使用 Dispatcher。
No you should not take the first approach, since it defeats the objective of calling a method in an async fashion.
Second approach is also troublesome since
There is no way to show a progress report for an async job unless async just provides it.
Solution is:
You must also be aware of the issues communicating with the UI thread from a background thread and using Invoke in Windows Forms and using Dispatcher in WPF.