c# - 进度栏值与Windows 7中呈现的内容不同步

标签 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?
               }
            );
         }
      }
   }
}


我的Google Kung Foo今天似乎死了。谢谢。

最佳答案

最后,这是与C# progress bar not synced with download (WebClient class)相同的问题。

此问题也与ProgressBar is slow in Windows Forms相似。此处的解决方案是向前和向后移动值,即progressBar1.Value = value + 1; progressBar1.Value = value;。第一次更新开始动画序列,而第二次更新强制动画序列提前停止。它是一个很好的解决方法,但是会引入难以重现的问题。似乎没有一个真正的解决方案。

最后,Disabling progress bar animation on Vista Aero建议关闭主题。这似乎可行,但是进度条失去了外观。最终,最好的办法就是创建自己的进度栏。

Microsoft应该只是将动画作为一种选择,而不是强迫开发人员重新发明进度条。

关于c# - 进度栏值与Windows 7中呈现的内容不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462978/

相关文章:

java - 如何将信息从 java 发送到 windows 任务栏到 javafx

ruby - 禁用将输出放入 RSpec

c# - 基于KeyValuePairs的过滤列表

c# - 获取 DefaultNetworkCredentials 以传递给 WCF 服务

c# - 将托管数组复制到非托管固定大小数组或从非托管固定大小数组复制

c# - 使用 DataGridView 的 WinForms 数据绑定(bind)

c# - 将 DLL 合并到程序集或嵌入到资源中 - 哪个更好?

android - AsyncTask、Fragments、Views 和 Backstacks

c# Web 应用程序如何获得不包括周末和公共(public)假期的工作日

c# - 在 C++ 和 C# 应用程序之间进行异步通信的最简单方法是什么