gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:57:27 24 4
gpt4 key购买 nike

似乎在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/

24 4 0