2015-09-01 66 views
-1

我有一个方法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(); 
} 
+2

出现这种情况的原因是因为你的努力更新和处理在UI线程上的文件。您需要创建一个线程来处理该文件,然后看到如何更新进度条以下内容http://stackoverflow.com/questions/6036279/wpf-progress-bar-update-with-dispatcher – 3dd

+1

请参阅此链接](http://stackoverflow.com/questions/12676490/progress-bar-not-progressing)和[this](http://www.dotnetcurry.com/ShowArticle.aspx?ID=571)调度员和后台工作者。 – learningNew

+0

http://stackoverflow.com/questions/6365887/can-you-link-to-a-good-example-of-using-backgroundworker-without-placing-it-on-a/6365951#6365951 – CharithJ

回答

1

基本上这个想法是,你不能直接从非UI线程更新UI元素。 WPF有一个Dispatcher类,插上U访问UI元素,并使用此您可以更新喜欢的UI元素下面的代码

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

@wudzik OP ??在这里没有看到任何其他代码。 – WAQ