进度条值与在Windows 7中呈现的内容不同步

Rod*_*ber 1 c# winforms progress-bar

似乎在Windows 7中,在设置进度条的值时会发生一些动画.设置值似乎不等待动画完成.有没有办法通知进度条何时完成动画?

我有一个示例程序.请参阅评论.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace Testing
{
   static class Program
   {
      [STAThread]
      static void Main()
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         var form = new Form1();
         form.Run();
      }
   }

   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      public void Run()
      {
         Thread thread = new Thread
         (
            delegate()
            {
               ProgressBarMax = 10;
               ProgressValue = 0;

               for (int i = 0; i < 10; i++)
               {
                  Thread.Sleep(1000);
                  ProgressValue++;
               }
            }
         );
         EventHandler show = delegate
         {
            thread.Start();
         };

         Shown += show;
         ShowDialog();
         Shown -= show;
      }

      public int ProgressBarMax
      {
         set
         {
            Invoke
            (
               (MethodInvoker)
               delegate
               {
                  progressBar1.Maximum = value;
               }
            );
         }
      }

      public int ProgressValue
      {
         get
         {
            return progressBar1.Value;
         }
         set
         {
            Invoke
            (
               (MethodInvoker)
               delegate
               {
                  label1.Text = value.ToString();
                  progressBar1.Value = value;

                  // setting the value is not blocking until the
                  // animation is completed. it seems to queue
                  // the animation and as a result a sleep of 1 second
                  // will cause the animation to sync up with the UI
                  // thread.

                  // Thread.Sleep(1000); // this works but is an ugly hack

                  // i need to know if there is a callback to notify me
                  // when the progress bar has finished animating.
                  // then i can wait until that callback is handled 
                  // before continuing.

                  // if not, do i just create my own progress bar?
               }
            );
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我的google kung foo今天似乎已经死了.谢谢.

Rod*_*ber 8

最后,这与C#进度条未与下载同步(WebClient类)的问题相同.

此问题类似于ProgressBar在Windows窗体中很慢.这里的解决方案是向前和向后移动值,即progressBar1.Value = value + 1; progressBar1.Value = value;.第一次更新开始动画序列,而第二次更新强制动画序列提前停止.它是一个很好的解决方案,但可以引入难以重现的问题.它似乎没有真正的解决方案.

最后,禁用Vista Aero上的进度条动画建议关闭主题.这似乎有效,但进度条失去了它的外观和感觉.最终,最好的办法就是制作自己的进度条.

微软应该只是让动画成为一种选择,而不是强迫开发人员重新发明进度条.

  • 我找到了最后一个值的变通方法:将进度条的最大值设置为最大值+ 1,然后将该值设置为相同的最大值,然后将最大值设置回其原始最大值.当条形图达到100%时,这将触发立即更新 (2认同)