简体   繁体   中英

Progress Bar not working properly in background worker

I need to have a progress bar to work(visible) at the time of the one process after that the visibility should be set to false. I am using a background worker for this process. but while using the visibility property the application is getting stalled other wise the application is running properly. I am using Devexpress progress bar .Please help me with this. This is the code which I am working.

private void Generate_Click(object sender, EventArgs e)
    {
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.RunWorkerAsync();
    }
private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int a = 0;
        int b = 0;
        ProgressBar.Visible = true;
        ProgressBar.Properties.Step = 1;
        ProgressBar.Properties.PercentView = true;
        ProgressBar.Properties.Maximum = SpecInformations.TotalSPCOCount;
        ProgressBar.Properties.Minimum = 0;
        Method_Call(a,b, sender as BackgroundWorker);
    }

    private void BackgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        ProgressBar.PerformStep();
        ProgressBar.Update();
    }

    private void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        ProgressBar.Visible = false;
    }

the code can help you to use progress bar in BackgroundWorker:

private void cmdButton_Click(object sender, EventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
        worker.RunWorkerAsync();
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 0; i < 101; i++)
        {
            worker.ReportProgress(i);
            System.Threading.Thread.Sleep(1000);
        }
    }

    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lblProgress.Text = ("Progress: " + e.ProgressPercentage.ToString() + "%");
    }

for additional info can use the link

You cannot access Windows controls like ProgressBar from inside the DoWork method or any method it has called because the thread that runs this code is a background thread and not the same thread that created the Control. You will get an exception whose message states that the control is being access by a thread other than the thread that created it, if you try. This is an inviolable rule about windows controls; they must always only be accessed by the thread that created them

The BackgroundWorker has a WorkerReportsProgress property that must be set to true, and a ReportProgress() method that you can call with an int (and pass an optional object for more info) of the percentage complete. When you call this method in your DoWork, the BackgroundWorker will automatically raise the ProgressChanged event and critically, it does so using the foreground thread it was created with(the same thread your other controls were created with) so code inside your ProgressChanged event handler is run using the proper thread and can access the ProgressBar Control without causing an exception

In summary:

  • Set WorkerReportsProgress to true
  • Call ReportProgress inside DoWork, passing a percentage complete or using the int to indicate the process has reached some stage (it doesn't have to be a percentage)
  • Attach an event handler to your worker's ProgressChanged event
  • Move your ProgressBar code to the ProgressChanged event handler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM