自动增加BackgroundWorker的ProgressBar

时间:2013-09-24 12:10:57

标签: c# wpf backgroundworker

如何自动增量ProgressBar而不是调用bgWorker.ReportProgress(x)? (我使用C#,WPF应用程序)。

问题是,在BackgroundWorker的工作中,我调用外部库做了很多工作,我想在库工作时更新ProgressBar。我可以估计运行外部方法所需的时间。

我尝试使用Timer,但这不起作用,因为我无法从另一个线程访问bgWorker。

换句话说,我需要这样的东西:

bgWorker.StartPercentage = 40;
bgWorker.FinishPercentage = 80;
bgWorker.ProgressBarUpdateTime = 20000; // estimated time needed to run external library
bgWorker.StartProgressBarUpdate();
ExternalLibrary.CallMethod(); // it takes about 20 sec to run this, bgWorker should
     // update ProgressBar, while method is working
bgWorker.ReportProgress(80); // correct ProgressBar value
// do rest of work

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您需要为更新进度条的计时器使用单独的后台工作程序。

答案 1 :(得分:0)

progressBar.BeginAnimation()正是我想要的。 @tim提供了帮助我的link

相关问题