Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress bar does not reach 100%

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;
like image 520
Daniel Peñalba Avatar asked May 05 '14 16:05

Daniel Peñalba


Video Answer


2 Answers

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;
like image 194
Daniel Peñalba Avatar answered Oct 02 '22 21:10

Daniel Peñalba


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.

like image 34
Michael Gunter Avatar answered Oct 02 '22 23:10

Michael Gunter