IAsyncresult - 轮询而不冻结 UI?

发布于 2024-11-07 07:50:14 字数 1592 浏览 5 评论 0原文

我有一个异步运行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回心转意 2024-11-14 07:50:14

我不确定您是否正确使用了异步模式。它应该看起来像这样:

void Start()
{
    System.IO.Stream s = ...;
    byte[] buf = ...;

    // start the IO.

    s.BeginRead(buf, 0, buf.Length, res =>
        {
            // this gets called when it's finished,
            // but you're in a different thread.

            int len = s.EndRead(res);

            // so you must call Dispatcher.Invoke
            // to get back to the UI thread.

            Dispatcher.Invoke((Action)delegate
                {
                    // perform UI updates here.
                });
        }, null);

    // the IO is started (and maybe finished) here,
    // but you probably don't need to put anything here.
}

Stream 编写,因为我不知道您的对象的签名,但希望您明白!您需要在给定的回调中处理操作的完成,而不是在调用 Begin 方法之后直接处理。您不需要轮询 IsCompleted 属性。

I'm not certain that you're using the async pattern correctly. It should look something like this:

void Start()
{
    System.IO.Stream s = ...;
    byte[] buf = ...;

    // start the IO.

    s.BeginRead(buf, 0, buf.Length, res =>
        {
            // this gets called when it's finished,
            // but you're in a different thread.

            int len = s.EndRead(res);

            // so you must call Dispatcher.Invoke
            // to get back to the UI thread.

            Dispatcher.Invoke((Action)delegate
                {
                    // perform UI updates here.
                });
        }, null);

    // the IO is started (and maybe finished) here,
    // but you probably don't need to put anything here.
}

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 the IsCompleted property.

独留℉清风醉 2024-11-14 07:50:14

不,您不应该采用第一种方法,因为它违背了以异步方式调用方法的目标。

第二种方法也很麻烦,因为

  • 你的进度报告是任意且不切实际的,
  • 即使是 100%,它也不能保证工作完成,因为在 100% 时,它不知道工作是否已经完成。

除非异步只提供它,否则无法显示异步作业的进度报告。

解决方案是:

  • 显示不确定进度条(也称为微调器)
  • 在回调内向用户报告结果。

您还必须注意从后台线程以及在 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

  • your progress reporting is arbitrary and unrealistic
  • even with 100% it does not guarantee that the work is done since at 100% it does not know if the work has been done.

There is no way to show a progress report for an async job unless async just provides it.

Solution is:

  • Show an indeterminate progress bar (also called spinner)
  • Report user of the result inside the callback

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文