C#Winform后台工作程序和进度条 [英] c# winform background worker and progress bar
问题描述
我正在尝试使用BG Worker来增加进度条. 我目前正在使用2个BG工人,一个将数据添加到数据库中,一个用于进度条.数据库上传工作正常,但进度条却没有.
I am trying to get the progress bar to increment using a BG Worker. I am currently using 2 BG workers, one to add data into a DB and one for the progress bar. The DB upload is working fine, yet the progress bar is not.
代码:
BackgroundWorker bg2 = new BackgroundWorker();
bg2.DoWork +=new DoWorkEventHandler(bg2_DoWork);
bg2.RunWorkerAsync();
void bg2_DoWork(object sender, DoWorkEventArgs e)
{
while (bg1.IsBusy)
DrawWellPlate.pbar.Increment(1)
}
bg1所指的是数据库上传线程,而pbar显然是进度条.
bg1 that it refers to is the database upload thread and pbar is clearly the progress bar.
谢谢.
推荐答案
您应该执行以下操作 其中totalProgress将显示在progressBar中,doWork不在UI线程中执行,这就是BackgroundWorker的目的
You should do something like this where totalProgress will be shown in progressBar, doWork is executed not in UI thread, that is the purpose of BackgroundWorker
BackgroundWorker bg2 = new BackgroundWorker();
bg2.DoWork +=new DoWorkEventHandler(bg2_DoWork);
.ProgressChanged += new ProgressChangedEventHandler(bg2_ProgressChanged)
bg2.RunWorkerAsync();
void bg2_DoWork(object sender, DoWorkEventArgs e)
{
while (bg1.IsBusy)
worker.ReportProgress(totalProgress);
}
private void bg2_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
DrawWellPlate.pbar.Value = e.ProgressPercentage;
}
有关更多详细信息,请参见此
see this for more details
这篇关于C#Winform后台工作程序和进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!