[英]progress bar in background worker get argument which being updated in the main thread c# wpf
I have the main thread which is wizard in WPF. 我有主线程是WPF中的向导。
after user finished set the properties of the wizard, it processing data. 用户完成设置向导的属性后,它将处理数据。
It takes a few seconds and I would like to raise a progress bar which report on the progress. 这需要几秒钟,我想提出一个进度条来报告进度。
Hence, I set always on the main thread variable call currentStep. 因此,我总是在主线程变量currentStep上进行设置。
I have totally thresholdStep steps which equals to 12. 我完全有thresholdStep步骤等于12。
So I want that the progress bar will work as a thread but it will also will be connected to the main thread by using currentStep variable. 因此,我希望进度条可以用作线程,但是也可以通过使用currentStep变量将其连接到主线程。
So, I used by background worker like this: 因此,我被后台工作人员使用过:
public partial class MessageWithProgressBar : Window
{
private BackgroundWorker backgroundWorker = new BackgroundWorker();
public MessageWithProgressBar()
{
InitializeComponent();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.ProgressChanged += ProgressChanged;
backgroundWorker.DoWork += DoWork;
backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
}
private void DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(100);
int i = (int)e.Argument;
backgroundWorker.ReportProgress((int)Math.Floor((decimal)(8*i)));
if (i > GeneralProperties.General.thresholdStep)
backgroundWorker.ReportProgress(100);
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progress.Value = e.ProgressPercentage;
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
WindowMsg msg = new WindowMsg();
msg.Show();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (backgroundWorker.IsBusy == false)
backgroundWorker.RunWorkerAsync(GeneralProperties.General.currentStep);
}
}
In addition, I called the background worker from the main thread as below: 另外,我从主线程中调用了背景工作者,如下所示:
MessageWithProgressBar progress = new MessageWithProgressBar();
progress.Show();
What acutally happens is that DoWork called only once with currentStep = 1 and it don't updates in relation to the main thread which also updated currentStep dependents on it's progress. 实际发生的是,DoWork仅在currentStep = 1时调用了一次,并且不会相对于主线程进行更新,该主线程也会根据其进度来更新currentStep。
Any ideas how to solve it? 有什么想法如何解决吗?
Thanks! 谢谢!
Change your DoWork
method like below: 如下更改您的
DoWork
方法:
private void DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(100);
int i = (int)e.Argument;
do
{
i = GeneralProperties.General.currentStep;
backgroundWorker.ReportProgress((int)Math.Floor((decimal)(8 * i)));
if (i > GeneralProperties.General.thresholdStep)
backgroundWorker.ReportProgress(100);
}
while (i < GeneralProperties.General.thresholdStep);
}
Just make sure your are not getting thread synchronization
problem with GeneralProperties.General
object, if you are then use lock
when accessing the object. 只要确保您在使用
GeneralProperties.General
对象时不会出现线程synchronization
问题,就可以在访问对象时使用lock
。
UPDATE: 更新:
For update problem: 对于更新问题:
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
progress.Value = e.ProgressPercentage;
}), null);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.