如何在繁忙循环期间显示进度?

我有一个循环,从外部源读取大量数据。 该过程大约需要20秒,我想向用户显示进度。 我不需要任何花哨的进度条,所以我选择在标签中绘制我的进度,该标签将显示“Step 1/1000”,然后更改为“Step 2/1000”等。

我的代码看起来像这样:

// "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 } 

但是,在该分析期间,屏幕“卡住”并且进度未显示为前进。 我从过去的C ++中理解这种行为,我可能会有一个单独的线程,显示进度条接收来自循环的通知,或某种forms的重绘/刷新,或强制窗口/应用程序处理其消息队列。

在C#中使用它的正确方法是什么? 我没有绑定标签,所以如果有一个简单的进度条弹出屏幕我可以使用而不是这个标签它也会很棒……

谢谢

将工作移动到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); } 
  //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; } 

UI未更新,因为您的当前线程的优先级高于最终设置标签的UI线程;)。 所以,直到你的线程完成你的东西,它将最终更新你的标签。

幸运的是,每个WPF控件上都有一个Dispatcher属性,可以让你激活另一个优先级的新线程。

 labelProgress.Dispatcher.Invoke(DispatcherPriority.Background, () => labelProgress.Content = string.Format("Step {0}{1}", i, countLabel)); 

这会在后台激活一个线程并完成工作! 您还可以尝试其他DispatcherPriority选项

PS我也冒昧地添加一个匿名方法并修复你的字符串解析…希望你不介意..