mo alaz
mo alaz

Reputation: 4749

progress bar using backgroundworker

There have been a bunch of questions on how to implement a progress bar and I believe I have been through all of them and I still don't know why my progress bar won't work.

Well, it seems to work only after the operation is done, which is of course not what I want. I want the progress bar to work as the main operation is doing its thing.

My code is pretty long and painful so I'm going to simplify it to make it as basic as possible.

private void button1_Click(object sender, EventArgs e)
{
  backgroundWorker1.RunWorkerAsync();
  //Main UI Operation
}

private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
 for (int i = 1; i <= 100; i++)
  {
   // Wait 100 milliseconds.
   Thread.Sleep(100);
   // Report progress.
   backgroundWorker1.ReportProgress(i);     }
  }

private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs        e)
  {
   progressBar1.Value = e.ProgressPercentage;
  }

Like I said, I only see the progress bar start after my main UI operation.

WorkerReportsProgress is set to True. Minimum and Maximum are 0 and 100 respectively.

I feel like this might be a lack of understanding on Threads.

Upvotes: 1

Views: 3495

Answers (1)

acadia
acadia

Reputation: 46

I tested the same code as you and found it worked. Did you forget to change the WorkerReportesProress property to True?

Upvotes: 1

Related Questions