I have this code:
for(int k = 0; k<11; k++)
{
pBar.Maximum = 10;
pBar.Value = k;
if (pBar.Maximum == k)
pBar.Value = 0;
}
If you switch to Classic mode, this glitch will be gone. The progress bar will appear fully drawn before being reset.
This is because in Classic mode, the painting operation is synchronous and completes before the Value
setter returns, but in the themed mode, there is some sort of animation played when you increase the value, and that takes some time to play.
On contrary, when you decrease the value, there is no animation; the progress bar is shrinked immediately.
This is why it appears only about 60% full: you decrease the value (which completes immediately) before the progress bar has time to draw the animation for the last several increments.
First: there is no any reason to assign pBar.Maximum
on every interarion.
Just do:
pBar.Maximum = 10;
for(int k = 0; k<11; k++)
{
pBar.Value = k;
if (pBar.Maximum == k)
pBar.Value = 0;
}
Second: your code result in blocking iteration. There is no way it could ever behave correctly. Use multi-threading and change the progress value
based on some event
,tick
whatever, not in loop, as it's done here.
pBar.Maximum = 10;
int count = 0;
Timer timer = new Timer();
timer.Interval = 1000;
timer.Tick += (source, e) =>
{
pBar.Value = count;
if (pBar.Maximum == count)
{
pBar.Value = 0;
timer.Stop();
}
count++;
}
timer.Start();
Your problem is that you're using a loop. You need to use a timer so that the program has time to both perform the checks/assignments and update the screen.
The code replaces the for loop with a timer that calls the body of the loop. Since a timer does not have an index variable, it is initialized outside of the timer (count) and updated with each tick.
I finally found a solution to this problem, and wrote about it here. The idea was from THIS SO question.
progressBar1.Value = e.ProgressPercentage;
if (e.ProgressPercentage != 0)
progressBar1.Value = e.ProgressPercentage - 1;
progressBar1.Value = e.ProgressPercentage;
if (progressBar1.Maximum == e.ProgressPercentage)
progressBar1.Value = 0;