【发布时间】: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();
}
【问题讨论】:
-
发生这种情况的原因是您尝试更新它并在 UI 线程上处理文件。你需要创建一个线程来处理文件然后看下面如何更新进度条stackoverflow.com/questions/6036279/…