【问题标题】:Updating progress bar in loop [duplicate]循环更新进度条[重复]
【发布时间】:2015-09-01 13:37:32
【问题描述】:

我有一个方法getPlugins 需要很长时间才能运行。本质上它正在解析一个非常大的日志文件。我知道日志文件从时间 0 到时间 24。我想根据当前时间更新 ProgressBar。这是我的代码结构,但该栏似乎只有在我的循环完成后才会更新......我该如何解决这个问题?

private void getPlugins(String filePath)
{
     var w = new Window2();
     w.Show();
     w.progress.Value = 0;

     List<String> pluginNames = new List<String>();

     string strLine;

     // Read the file and display it line by line.
     System.IO.StreamReader file = new System.IO.StreamReader(filePath);

      while ((strLine = file.ReadLine()) != null)
      {
          // Do stuff....

          float time; // Here I have time as a float from 0 to 24
          w.progress.Value = time;
      }

      file.Close();
      w.progress.Value = 24;
      w.Close();
}

【问题讨论】:

标签: c# wpf


【解决方案1】:

基本上,您不能直接从非 UI 线程更新 UI 元素。 WPF 有一个 Dispatcher 类,可让您访问 UI 元素,使用它您可以更新 UI 元素,如下面的代码

Application.Current.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, (Action)(() =>
                    {
                        w.progress.Value = time;
                    }));

【讨论】:

  • @wudzik OP??在这里看不到任何其他代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
相关资源
最近更新 更多