背景工作者不报告在wpf的进展

时间:2012-10-11 06:59:34

标签: wpf progress-bar backgroundworker progress

  

可能重复:
  Backgroundworker won’t report progress

我在WPF中使用Background worker。问题是它没有报告进度。它只是在完成任务时更新ProgressBar。我想在后台任务运行时定期更新它。这是我的示例代码。

  bworker_doWork(...)
    {
       BindData();//this is database operation which binds data to datagrid.
    }

    bWorker_ProgressChanged(...)
    {
       progressBar.Value=e.ProgressPercentage;//it doesnt update this value
    }

    bWorker_RunWorkerCompleted(..)
    {
       progressBar.Value=100;//max value. control comes here and updates the progress to max value.
    }

1 个答案:

答案 0 :(得分:-1)

您是否将ProgressPercentage设置在任何位置? (无法从您提交的代码中判断出来)

我认为您错过的是您必须在代码中调用ReportProgress;就这样:

myBackgroundWorker.ReportProgress((int)percent, null);

我猜你的代码看起来像这样:

bworker_doWork(...)
{
   BindData();//this is database operation which binds data to datagrid.
   bworker.ReportProgress((int)percent);
}

bWorker_ProgressChanged(...)
{
   progressBar.Value=e.ProgressPercentage;//it doesnt update this value
}

bWorker_RunWorkerCompleted(..)
{
   progressBar.Value=100;//max value. control comes here and updates the progress to max value.
}