BackgroundWorker.ReportProgress 令人困惑的例子?

发布于 2024-09-06 23:41:17 字数 1597 浏览 2 评论 0原文

BackgroundWorker 的几乎所有教程中,reportProgress 事件都是这样处理的(此示例来自 MSDN http://msdn.microsoft.com/en-us/library/cc221403(VS.95).aspx)

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;

  for (int i = 1; (i <= 10); i++)
  {
    if ((worker.CancellationPending == true))
    {
        e.Cancel = true;
        break;
    }
    else
    {
        // Perform a time consuming operation and report progress.
        // _results.Load() downloads XML and save the data to database
        System.Threading.Thread.Sleep(500);
        worker.ReportProgress((i * 10));
    }
  }
}

我的函数下载 XML 并将其保存到解析后的数据库。我在下面调用这个函数“//执行一个耗时的操作并报告进度”。但我的函数不会运行 10 次吗?

后来我修改了 Load(),添加了变量 CountTotal(结果总数)和 CountLoaded(保存的结果数,它随着函数的进展而变化)。

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;

  for (int i = 1; (i <= 10); i++)
  {
    if ((worker.CancellationPending == true))
    {
        e.Cancel = true;
        break;
    }
    else
    {
        // Perform a time consuming operation and report progress.
        _results.Load() downloads XML and save the data to database

        worker.ReportProgress((_results.CountLoaded * 10)); //i did something like this
    }
  }
}

问题是 worker.ReportProgress_results.Load() 完成后执行。如何解决这个问题呢?互联网上给出的示例是否真的很糟糕,因为它们建议循环调用该函数,或者我弄错了?

In almost all tutorials of BackgroundWorker the reportProgress event is handled like this (this example is from MSDN http://msdn.microsoft.com/en-us/library/cc221403(VS.95).aspx)

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;

  for (int i = 1; (i <= 10); i++)
  {
    if ((worker.CancellationPending == true))
    {
        e.Cancel = true;
        break;
    }
    else
    {
        // Perform a time consuming operation and report progress.
        // _results.Load() downloads XML and save the data to database
        System.Threading.Thread.Sleep(500);
        worker.ReportProgress((i * 10));
    }
  }
}

My function downloads XML and save it to database after parsing. I called this function below "// Perform a time consuming operation and report progress." But won't my function run 10 times?

Later i modified Load() adding to variables CountTotal (total number of results) and CountLoaded (number of results saved, it changes as the function progress).

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;

  for (int i = 1; (i <= 10); i++)
  {
    if ((worker.CancellationPending == true))
    {
        e.Cancel = true;
        break;
    }
    else
    {
        // Perform a time consuming operation and report progress.
        _results.Load() downloads XML and save the data to database

        worker.ReportProgress((_results.CountLoaded * 10)); //i did something like this
    }
  }
}

The problem is that worker.ReportProgress executes after the completion of _results.Load(). How to solve this problem? Are the given examples on internet really bad because they are suggesting to call the function in a loop, or I got them wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

秋日私语 2024-09-13 23:41:17

是的 - 这将执行 Load 10 次。该示例的目的是说明当您可以估计总体工作负载或报告有意义的进度时的用法。他们只是试图通过进度指示来模拟“一些工作”。

如果您不能这样做,那么只需异步运行它(通过BackgroundWorker),但显示滚动的无限选取框或类似的内容。不要执行 10 次;p 或者,运行 Load,然后在处理数据时报告进度。假设这需要一些时间。如果所有时间都在Load中,那么...

Yes - that will execute Load 10 times. The intent in that example is to illustrate usage when you can estimate the overall workload, or report meaningful progress. They are just trying to simulate "some work" with progress indication.

If you can't do that, then just run it async (via BackgroundWorker) but show a scrolling infinite marquee or similar. Don't do it 10 times ;p Alternatively, run Load, then report progress when you process the data. Assuming that takes some time. If all the time is in Load, then...

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