Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; })); 
like image 737
Lasse V. Karlsen Avatar asked Mar 16 '11 22:03

Lasse V. Karlsen


People also ask

How do I stop progressBar animation?

To stop the progress animation, consider using the clearInterval() method which allows you to stop the underlying interval that is driving your incremental animation.

How do I set progressBar value?

You can update the percentage of progress displayed by using the setProgress(int) method, or by calling incrementProgressBy(int) to increase the current progress completed by a specified amount. By default, the progress bar is full when the progress value reaches 100.

How does progressBar work in C#?

A progress bar is a control that an application can use to indicate the progress of a lengthy operation such as calculating a complex result, downloading a large file from the Web etc. ProgressBar controls are used whenever an operation takes more than a short period of time.

How do I set progressBar text?

Alliteratively you can try placing a Label control and placing it on top of the progress bar control. Then you can set whatever the text you want to the label.


1 Answers

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!

like image 89
David Heffernan Avatar answered Sep 22 '22 16:09

David Heffernan