ProgressBar在Windows窗体中很慢

use*_*327 15 .net c# winforms

我正在使用Windows Vista和Visual Studio 2010.创建.Net 4 Windows窗体应用程序.删除默认表单上的进度条,添加代码以处理表单加载事件并执行progressBar1.Value = 100;此操作.

开始调试,你会看到一个动画,在大约半秒钟内将进度条移动到100.

我的项目需要2个进度条.一个用于"全局进展",第二个用于"当前步进",因此第二个从0到100,并且回到0以进行下一步.问题是,由于进度条对于某些快速步骤而言速度很慢,因此它永远不会达到100并且看起来很奇怪.

有没有办法摆脱那个动画?在WPF中没关系,但我宁愿继续使用Windows Forms.

Dav*_*nan 15

这就是Vista/7进度条的设计方式.当您更改进度条的值时,该栏会逐渐动画到该值.

我知道避免此问题的唯一方法是在更新进度条时倒退,如下所示:

progressBar1.Value = n;
if (n>0)
    progressBar1.Value = n-1;
Run Code Online (Sandbox Code Playgroud)

有关更完整的讨论,请参阅更改值时禁用.NET进度条动画?


Der*_*k W 15

建立关的赫弗南的尖上的进度条和倒退莱因哈特的扩展方法的方法在一个相关的问题,我想到了我自己的解决方案.

该解决方案非常无缝,并成功处理了值所在的问题Maximum.这种扩展方法可以ProgressBar缓解在Windows Vista和7上运行时WinForms控件中出现的渐进式动画样式导致的滞后(我还没有在Windows 8上测试过). ProgressBar

public static class ExtensionMethods
{
    /// <summary>
    /// Sets the progress bar value, without using 'Windows Aero' animation.
    /// This is to work around a known WinForms issue where the progress bar 
    /// is slow to update. 
    /// </summary>
    public static void SetProgressNoAnimation(this ProgressBar pb, int value)
    {
        // To get around the progressive animation, we need to move the 
        // progress bar backwards.
        if (value == pb.Maximum)
        {
            // Special case as value can't be set greater than Maximum.
            pb.Maximum = value + 1;     // Temporarily Increase Maximum
            pb.Value = value + 1;       // Move past
            pb.Maximum = value;         // Reset maximum
        }
        else
        {
            pb.Value = value + 1;       // Move past
        }
        pb.Value = value;               // Move to correct value
    }
}
Run Code Online (Sandbox Code Playgroud)

样品用法:

private void backgroundWorker_ProgressChanged(object sender, 
                                                  ProgressChangedEventArgs e)
{
     progressBar.SetProgressNoAnimation(e.ProgressPercentage);
}
Run Code Online (Sandbox Code Playgroud)