开发者

C# windows form progress bar with background worker

I am attempting to add a progress bar in my c# excel add in. The progress bar appears but it does not indicate any progress until the function is finished executing.

These are the functions in the Windows Form Class:

    public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {

            Thread.Sleep(100);

            backgroundWorker1.ReportProgress(i); //run in back thread


        }

    }



    public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) //call back method
    {

        progressBar1.Value = e.ProgressPercentage;

    }

    public void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) //call back method
    {

        progressBar1.Value = progressBar1.Maximum;

    }

This is how I call the function from the add in button:

    private void buttonClicked(object sender, RibbonControlEventArgs e)
 开发者_如何学运维   {
        AddInPlanView.Form1 pBar = new AddInPlanView.Form1();

        pBar.Visible = true;

        pBar.backgroundWorker1.WorkerReportsProgress = true;

        pBar.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(pBar.backgroundWorker1_ProgressChanged);

        pBar.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(pBar.backgroundWorker1_DoWork);

        pBar.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(pBar.backgroundWorker1_RunWorkerCompleted);

        pBar.backgroundWorker1.RunWorkerAsync();

        FUNCTIONTORUN();

        pBar.Visible = false;

    }

How should I change this so that the progress bar will not stop working during the execution of FUNCTIONTORUN()? The progress bar is initialized using the Visual Studio designer. The name of the progressbar object in the form class is progressbar1.

Thank YOU!


The DoWork event for your progress bar should be (or contain) FUNCTIONTORUN(). FUNCTIONTORUN() should use the ReportProgress method.


What does FUNCTIONTORUN do? It is very likely blocking the UI thread, causing your updates to the progress bar to not be seen until it returns. More than likely, FUNCTIONTORUN needs to run in a background worker, and have it periodically report progress, and that's where you'd update the progress bar.


I would like to expand on the following statement. Matt Greer can correct me if I am wrong.

What does FUNCTIONTORUN do? It is very likely blocking the UI thread, causing your updates to the progress bar to not be seen until it returns. More than likely, FUNCTIONTORUN needs to run in a background worker, and have it periodically report progress, and that's where you'd update the progress bar.

What I believe he means by this is you need to update progressBar1.Value within FUNCTIONTORUN(); you can still use a similar method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜