Alexey
Alexey

Reputation: 3637

Implementing a Progress Bar in C#

Attempt number 2. Trying to be as clear as possible because I have been on this task for much longer than I should have and made little progress.

I need to make a progress bar for an application. Right now, everything is happening in one UI thread, all the processing and whatnot, so when I do the long running process after a click of a button, the program hangs for roughly 40 seconds, then resumes with the output (I cannot change that part, the application was given to me). And I also have to make a cancel button, so if it is hit during the intermediate process, after that process is done, it checks for the Cancel flag, and if it is ON, break out of all the methods and return to initial position.

What I have to do is make a progress bar for this process. Two of them, one for intermediate process and one for total. Intermediate is a small action, like DB call, output intermediate process (for and foreach loops).

I have tried putting the bars in the same UI window and update them in the same thread as I go on (find major points, start the progress bars there and increment them based on my judgement. And loops). That was just loading the UI even more, but it was working (choppy progress bar movements).

Then I tried using the background worker. Same thing, put the bars in the UI and run them through the background worker. The problem with that even though the BG worker runs, the refresh must happen in the UI window, which is busy processing the greedy request, so it is not updated when I want it to.

I tried using multi threading at core (no BG worker) and create a new form in a new thread that would reflect the progress, but I was cautioned not to do it (running 2 UI threads at the same time) and I had troubles passing values around. Not to mention if using the threading method it is impossible to get an accurate representation of the progress happening without completely overloading the CPU (have another thread run in a while(true) and do an update when you call the updateMethod from some point in the process)

Do I have any other options left? Because the least negative aspect is the first where I update the progress bar as I go along with the program.

Upvotes: 3

Views: 1630

Answers (3)

Servy
Servy

Reputation: 203802

When using a background worker you should be doing all of the non-UI processing in the background worker; you should not be using it to update the UI while still doing processing in the UI thread.

By moving the work to the DoWork handler of the BGW you free up the UI thread to allow it to process UI events, including the updating of the progress bar.

Upvotes: 2

Fabian Bigler
Fabian Bigler

Reputation: 10895

Do I have any other options left? Yes and no. The best option is still to put time to learn how to use the backgroundworker properly, it's by far the easiest solution for this problem.

Go ahead and study these links:

Most important thing is to set this property:

bw.WorkerReportsProgress = true;

And in the DoWork-Function, make sure you report the progress to the UI, as given in the linked examples:

worker.ReportProgress((i * 10));

Also, don't forget to handle the progresschanged event in the UI:

private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
}

Go ahead, and you will see: it is fairly simple.

Upvotes: 3

SaravanaKumar
SaravanaKumar

Reputation: 747

private void btnCreate_Click(object sender, EventArgs e)
  {
      Progressbar1.Minimum = 1;

      Progressbar1.Maximum = dt.Rows.Count;

      Progressbar1.Value = 1;

      Progressbar1.Step = 1;


     for (int i = 1; i <= dt.Rows.Count; i++)
     {
            bool result=InsertUserdetails(Username);

            if(result== true)
            {
                 Progressbar1.PerformStep();
             }
     }
  }

Upvotes: -2

Related Questions