Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
954 views
in Technique[技术] by (71.8m points)

winforms - Disabling .NET progressbar animation when changing value?

I realize there are other questions on SO regarding animations and progressbars, but they seem to revolve around getting rid of the animation drawn on top of the progress bar, ie. the highlight that travels over it.

What I want to do is to get rid of the animation that is used when I set the new value of the progress bar. The problem I have now is that the action that is running completes and then the progress bar continues to increase up to their max position after the action has completed.

In other words, if I set the Value property of the progressbar to 50, I want it to travel to the halfway position (if max is 100) immediately, not slowly build up the progressbar to that position as it does now.

If there is indeed a question on SO that already deals with this, just close as duplicate and I'll happily delete it, but I could not find any.

This is the one I found: Disabling WinForms ProgressBar animation, and it deals with the highlight that is animated, and that's not what I'm talking about.

Here's a simple LINQPad demo that shows the problem:

void Main()
{
    using (var fm = new Form())
    {
        var bt = new Button
        {
            Text = "Start",
            Location = new Point(8, 8),
            Parent = fm,
        };
        var pb = new ProgressBar
        {
            Top = bt.Top + bt.Height + 8,
            Width = fm.ClientRectangle.Width - 16,
            Left = 8,
            Parent = fm
        };

        bt.Click += (s, e) =>
        {
            bt.Enabled = false;
            Thread t = new Thread(new ThreadStart(() =>
            {
                Thread.Sleep(1000);
                bt.BeginInvoke(new Action(() => { pb.Value = 50; }));
                Thread.Sleep(1000);
                bt.BeginInvoke(new Action(() => { pb.Value = 100; }));
                bt.BeginInvoke(new Action(() => { bt.Enabled = true; }));
            }));
            t.Start();
        };
        fm.ShowDialog();
    }
}

Edit 1: This is Windows 7, Glass theme, so yes, I bet this is specific to 7 or possibly also Vista.

Here's a GIF-animation that shows the problem, the project from above. You can see that as soon as the button becomes enabled, 1 second after the halfway mark has been set, the progressbar animates up to 100%, after the button has become enabled.

As you can see above, setting the button back to enabled and setting the progressbar to 100 is done "at the same time". Basically, I don't want the progressive buildup of the progressbar, I want it to jump directly to 50% and then to 100% at the same time as the button becomes enabled.

LINQPad demo


Edit 2: In response to David Heffernan's answer, this is how I changed the above code:

bt.BeginInvoke(new Action(() => { pb.Value = 51; pb.Value = 50; }));
Thread.Sleep(1000);
bt.BeginInvoke(new Action(() => { pb.Maximum = 101; pb.Value = 101;
                                  pb.Maximum = 100; pb.Value = 100; }));
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This animation feature was introduced in Vista with the Aero theme.

There is a workaround though. If you move the progress backwards, the animation is not shown. So if you want it to advance by 50 instantly, increment Value by 51, then immediately decrement by 1.

You get into strife when close to 100% because you can't set Value to 101 (I'm assuming Maximum is set to 100). Instead set Maximum to 1000, say, increase to 1000, decrease to 999, and then move back to 1000.

Anyway, it's kind of weird, but it does have the benefit of giving you the desired effect!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...