C# 如何在繁忙循环期间显示进度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1194620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I display progress during a busy loop?
提问by Roee Adler
I have a loop that reads plenty of data from an external source. The process takes about 20 seconds, and I want to show the progress to the user. I don't need any fancy progress bars, so I chose to plot my progress in a label that will say "Step 1/1000", then change to "Step 2/1000" etc.
我有一个从外部源读取大量数据的循环。该过程大约需要 20 秒,我想向用户显示进度。我不需要任何花哨的进度条,所以我选择在一个标签中绘制我的进度,该标签会显示“第 1/1000 步”,然后更改为“第 2/1000 步”等。
My code looks something like this:
我的代码看起来像这样:
// "count" is the number of steps in the loop,
// I receive it in previous code
String countLabel = "/"+count.ToString();
for (i = 0; i < count; i++)
{
... do analysis ...
labelProgress.Content = "Step "+i.ToString()+countLabel
}
However, during that analysis the screen is "stuck" and the progress does not show as advancing. I understand this behavior from my past in C++, where I would probably have a separate thread showing a progress bar receiving notifications from the loop, or some form of repaint/refresh, or forcing the window/app to process its message queue.
但是,在该分析期间,屏幕“卡住”并且进度未显示为前进。我从我过去在 C++ 中理解这种行为,在那里我可能会有一个单独的线程显示从循环接收通知的进度条,或某种形式的重绘/刷新,或强制窗口/应用程序处理其消息队列。
What's the right way to do it in C#? I'm not tied to the label, so if there's a simple progress-bar popup screen I could use instead of this label it would also be great...
在 C# 中执行此操作的正确方法是什么?我没有被标签束缚,所以如果有一个简单的进度条弹出屏幕,我可以用它来代替这个标签,它也会很棒......
Thanks
谢谢
采纳答案by user7116
Move the work to a BackgroundWorkerand use the ReportProgressmethod.
将工作移至BackgroundWorker并使用ReportProgress方法。
for (i = 0; i < count; i++)
{
... do analysis ...
worker.ReportProgress((100 * i) / count);
}
private void MyWorker_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
taskProgressBar.Value = Math.Min(e.ProgressPercentage, 100);
}
回答by Arcturus
The UI is not updated due to the fact that your current thread has a higher priority than your UI thread which will ultimately set the label ;). So until your thread has finished to your stuff it will update your label in the end.
由于您当前线程的优先级高于最终将设置标签的 UI 线程,因此 UI 不会更新;)。因此,在您的线程完成您的内容之前,它最终会更新您的标签。
Lucky for us, there is a Dispatcher property on every WPF control that lets you fire up a new thread with another priority..
幸运的是,每个 WPF 控件上都有一个 Dispatcher 属性,可让您启动具有另一个优先级的新线程。
labelProgress.Dispatcher.Invoke(DispatcherPriority.Background,
() => labelProgress.Content = string.Format("Step {0}{1}", i, countLabel));
This fire up a thread in the background and would get the job done! You can also try other DispatcherPriorityoptions
这会在后台启动一个线程并完成工作!您还可以尝试其他DispatcherPriority选项
PS I also took the liberty to add an anonymous method and fix your string parsing somewhat.. hope you don't mind..
PS我还冒昧地添加了一个匿名方法并稍微修复了您的字符串解析..希望您不介意..
回答by Dan
//Create a Delegate to update your status button
delegate void StringParameterDelegate(string value);
String countLabel = "/" + count.ToString();
//When your button is clicked to process the loops, start a thread for process the loops
public void StartProcessingButtonClick(object sender, EventArgs e)
{
Thread queryRunningThread = new Thread(new ThreadStart(ProcessLoop));
queryRunningThread.Name = "ProcessLoop";
queryRunningThread.IsBackground = true;
queryRunningThread.Start();
}
private void ProcessLoop()
{
for (i = 0; i < count; i++)
{
... do analysis ...
UpdateProgressLabel("Step "+i.ToString()+countLabel);
}
}
void UpdateProgressLabel(string value)
{
if (InvokeRequired)
{
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new StringParameterDelegate(UpdateProgressLabel), new object[] { value });
return;
}
// Must be on the UI thread if we've got this far
labelProgress.Content = value;
}