【发布时间】:2015-06-14 09:32:02
【问题描述】:
我想刷新 C# WPF 中的进度条。
这个问题听起来很简单,我应该可以用谷歌搜索它。但是我看到的解决方案让我不满意。
假设我必须运行一个很长的算法,例如5个不同的步骤。 我不知道计算不同的步骤需要多长时间。 但我知道我已经编写了什么程序,我可以使用分析器来检查 CPU 用于每个步骤的时间(以所有步骤总时间的百分比)。
这可能是时代例如:
Method1() takes 3s
Method2() takes 5s
Method3() takes 1s
这是我的方法:
“简单”的方法:
ProgressBar pb = new ProgressBar()
{
// The total duration of all methods
Maximum = 9
};
Method1();
// + 3 for 3 seconds
pb.Value += TimeForMethod1;
Method2();
// + 5 for 5 seconds
pb.Value += TimeForMethod2;
Method3();
// + 1 for 1 second
pb.Value += TimeForMethod3;
这很容易。但是有问题。这会阻塞我的 UI 线程 9 秒,这太可怕了(因为用户可能认为程序崩溃了)。
所以使用线程似乎很明显..
“线程”方法:
这有一个问题,我需要调度 ProgressBar 上的每个操作,这可能非常慢(对于 ProgressBar 上的大量更新)
我为此写了一个“任务队列”。我可以将我想做的所有工作保存在 Queue 中,Thread 在调用 Run 并更新任务之间的 ProgressBar(和 Label)之后正在处理所有这些任务。
我不想发布ThreadQueue 的所有代码,因为它已经实现了很多而且可能还没有那么好实现(还)。
这是线程方法的重要部分:
foreach (var threadQueueNode in threadQueue)
{
// Changes e.g. a label displaying what the thread is doing next
threadQueueNode.PreMainFunction.Invoke();
// Executes the main task
this.Result = threadQueueNode.MainFunction.Invoke();
// Updates the ProgressBar after the work is done.
threadQueueNode.PostMainFunction.Invoke();
}
PostMainFunction 是 Delegate 和例如这个:
PostMainFunction = (Action<int>)((value) => this.Dispatcher.Invoke(() => this.ProgressBarStatus.Value += value));
对于像我这样的问题,更新 ProgessBar 的专业方法是什么?
我很乐意讨论。
感谢您的帮助和时间!
【问题讨论】:
-
为什么不使用Background Worker?
标签: c# wpf multithreading progress-bar