大廈關閉的Heffernan's tip上在一個相關的問題的進度條和Reinhart's extension method approach倒退,我想到了我自己解。
的解決方案是相當無縫和成功處理當值處於Maximum
你會遇到的問題。 ProgressBar
的這種擴展方法減輕了滯後,這是由於在Windows Vista和7(我尚未在Windows 8上測試過)上運行時,存在於漸進式動畫風格中的漸進式動畫風格導致的。
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
}
}
使用範例:
private void backgroundWorker_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
progressBar.SetProgressNoAnimation(e.ProgressPercentage);
}
嘗試調用Application.DoEvents()方法 – 2011-05-20 12:13:24
我不認爲這是Windows窗體的問題。我多次使用進度條,並且總是很快。檢查其他內容是否會降低應用程序的性能。 – Vale 2011-05-20 12:13:58
只有進度條纔有問題。我正在使用一個背景工作,報告進度在0到100之間循環,每個值之間都有一個System.Threading.Thread.Sleep(10)。報告進度的事件處理程序在文本框和進度條中顯示進度的值。文本框從0到100,進度條從0到大約70. – user755327 2011-05-20 12:16:32