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.
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; }));
Solution 1:
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!
Solution 2:
Here is my extension method, based on David Heffernan's recommendation:
Wrap it up, hide it from view, and pretend it's not there!
public static class ExtensionMethods
{
/// <summary>
/// Sets the progress bar value, without using Windows Aero animation
/// </summary>
public static void SetProgressNoAnimation(this ProgressBar pb, int value)
{
// Don't redraw if nothing is changing.
if (value == pb.Value)
return;
// To get around this animation, we need to move the progress bar backwards.
if (value == pb.Maximum) {
// Special case (can't set value > Maximum).
pb.Value = value; // Set the value
pb.Value = value - 1; // Move it backwards
}
else {
pb.Value = value + 1; // Move past
}
pb.Value = value; // Move to correct value
}
}
Solution 3:
There is another way to skip the animation of a vista-style progress bar:
Just SetState()
the control to PBST_PAUSED
, then set the value and finally set it back to PBST_NORMAL
.