(Sorry for the not-so-good-title, I’m not sure how my problem can be solved, thus what is the question I need to ask 🙂
Context
I use a progress bar to inform the user of the progress of a task. The task is in fact done in two steps, each of them taking approximately half of the total time to be performed. I only know the length of the second task just before starting it (because it depends on the results of the previous task), so I can’t know the maximum progress at the very beginning. That is why I change the maximum progress of my progress bar before the second task.
This is basically how I do it:
// 1st step
progressBar.Maximum = step1Objects.Count * 2; // "2" because the step will take
// half of the total process
progressBar.Value = 0;
foreach (SomeObject step1Object in step1Objects) {
// Build step2Objects
progressBar.Value = ++progress;
}
// At this moment, the progress bar is half filled
// 2nd step
progressBar.Maximum = step2Objects.Count * 2;
progressBar.Value = step2Objects.Count;
// When we start this step, the progress bar is already half filled
foreach (SomeObject step2Object in step2Objects) {
// Do something
progressBar.Value = ++progress;
}
// At this moment, the progress bar is totally filled
Problem
When I get to this line:
progressBar.Maximum = step2Objects.Count * 2;
… the progress bar is for a short instant not half filled anymore because step1Objects.Count is very little compared to step2Objects.Count. So the progress bar does something like that (that’s what the user sees):
> |
=> |
==> |
===> |
====> |
=====> | End of step 1
=> | progressBar.Maximum = step2Objects.Count * 2;
=====> | progressBar.Value = step2Objects.Count;
======> |
=======> |
========> |
=========> |
==========>|
Question
How can I avoid this “glitch”?
I think that the thing to do is stop the refresh of the progress bar between the two steps. I was thinking at something like BeginUpdate/EndUpdate, but it doesn’t seem to exist for progress bars…
You write that both tasks are approximately half of the total time to be performed.