Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
682 views
in Technique[技术] by (71.8m points)

c# winform background worker and progress bar

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.

Code:

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 that it refers to is the database upload thread and pbar is clearly the progress bar.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...