Progress bar does not reach 100%

11,686

Solution 1

According to the following David Heffernan's answer, the issue is caused by the animation added in Windows 7. The issue gets fixed doing the following trick:

                progressBar1.Value = mValue;
                progressBar1.Value = mValue - 1;

Solution 2

This is happening because there is animation built into the WinForms ProgressBar control. To see this for yourself, just change your animate_scroll method like so:

    private void animate_scroll(object sender, EventArgs e)
    {
        progressBar1.Minimum = 0;
        progressBar1.Maximum = 100;
        progressBar1.Step = 1;
        // ThreadPool.QueueUserWorkItem(new WaitCallback(AnimateScroll));

        // here's the change:
        progressBar1.Value = 100;
    }

As you'll see, when you programmatically change the value from 0 (the default) to 100, the ProgressBar control animates itself to expand its content to the width that corresponds to the value. Your original code is sending Value updates faster than the control can animate between the values.

There doesn't seem to be a way to change the way this animation works. Maybe your use case would be satisfied just by setting the Style property to Marquee, which scrolls a highlighted section across the control.

Solution 3

I am doing the same type of thing to test my progress bar before the actual function gets implemented. I estimated my final function will take around 5 seconds to complete, so I set up the progress bar to have a maximum of 100 and a step of 1. Then I created a loop to 100 and did a Thread.Sleep(50) each loop for about 5 seconds of time.

for( int ii = 0; ii < 110; ++ii)
{
    Thread.Sleep(50);
    form.statusOutputMainChannel(ii.ToString());
    form.progressBarVerifyUpdate();
}

I found that I had to call the bar.PerformStep() method another 10 times in order for the animation to catch up to the value.

This is another "ugly" way to get it to work, but it's not clear to me that the other answers really got to a "this fixes it" either.

  • Rob
Share:
11,686
Daniel Peñalba
Author by

Daniel Peñalba

Software Engineer at Unity Technologies, Valladolid, Spain. Currently developing gmaster, Plastic SCM and SemanticMerge. Areas: C# GUI development Winforms and WPF expert ASP .NET Core Multiplatform UI development with Mono (Linux and OSX, GTK# and MonoMac) Eclipse plugin, Java Automated testing, NUnit, Moq, PNUnit and TestComplete Email: dpenalba[AT]codicesoftware[DOT]com I play the guitar at Sharon Bates, the greatest Spanish rock band.

Updated on June 08, 2022

Comments

  • Daniel Peñalba
    Daniel Peñalba almost 2 years

    I'm performing some tests with an scrollbar in Winforms. I have the following code, that seems to be correct, but I never see the scrollbar completely fill. When the scrollbar value reaches 100, the scroll draw is as shown in this picture (about 50%).

    enter image description here

    This is the example code that I'm using:

        private void animate_scroll(object sender, EventArgs e)
        {
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Step = 1;
    
            ThreadPool.QueueUserWorkItem(new WaitCallback(AnimateScroll));
        }
    
        private void AnimateScroll(object state)
        {
            while (true)
            {
                mValue = (mValue + 1) % 101;
                this.Invoke((MethodInvoker)delegate()
                {
                    progressBar1.Value = mValue;
                    System.Diagnostics.Debug.WriteLine(progressBar1.Value);
                });
                System.Threading.Thread.Sleep(10);
            }
        }
    
        private int mValue = 0;