各位前辈大家好,我刚写C#程序,遇到点问题请多赐教。
想做日志查询功能,由于日志文件比较大,几万行,所以考虑将费时的操作放在子线程内执行,使用streamReader读文件的每一行,一旦符合条件调用BeginInvoke通知textBox,然后更新显示文本。
但是仍然卡死,原因在哪呢?
这是主窗口
[C#] 纯文本查看 复制代码
logTextBox1 = new logTextBox();
foreach (string logFilePtah in Paths)
{
Thread objThread = new Thread(new ThreadStart(delegate
{
logTextBox1.logFilter(logFilePtah, logFilterCriteria);
}));
objThread.Start();
}
logTextBox1.Show();
点击按钮会弹出查询结果的新窗口
[C#] 纯文本查看 复制代码 public void logFilter(String LogFilePath, LogFilterCriteria logFilterCriteria)
{
try
{
if (File.Exists(LogFilePath))
{
using (FileStream fs = new FileStream(LogFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs))
{
string nextLine;
while ((nextLine = sr.ReadLine()) != null)
{
if (logFilterCriteria.ModuleName != null)
{
//add ".(" to filter some unexpected situation
if (!nextLine.Contains(logFilterCriteria.ModuleName + ".(")) continue;
}
if (logFilterCriteria.FuncState != null)
{
if(logFilterCriteria.FuncState != "EXIT FUNCTION")
{
if (!nextLine.Contains(logFilterCriteria.FuncState)) continue;
}
else
{
if (!nextLine.Contains(logFilterCriteria.FuncState)) { continue; }
else if (nextLine.Contains("NORMAL")) { continue; }
else { }
}
}
if (logFilterCriteria.WarningLevel != null)
{
if (!nextLine.Contains(logFilterCriteria.WarningLevel)) continue;
}
if (logFilterCriteria.Keyword1 != null)
{
if (!nextLine.Contains(logFilterCriteria.Keyword1)) continue;
}
if (logFilterCriteria.Keyword2 != null)
{
if (!nextLine.Contains(logFilterCriteria.Keyword2)) continue;
}
if (logFilterCriteria.Checked1 || logFilterCriteria.Checked2)
{
Regex regDateTime = new Regex(@"[\d]{4}\-[\d]{2}\-[\d]{2}\s[\d]{2}\:[\d]{2}\:[\d]{2}");
Match matDateTime = regDateTime.Match(nextLine);
if (matDateTime.Success)
{
dtDateTime = Convert.ToDateTime(matDateTime.Value);
}
else
{
continue;
}
if (logFilterCriteria.Checked1 && !logFilterCriteria.Checked2)
{
if (DateTime.Compare(dtDateTime, logFilterCriteria.StartTime) < 0) { continue; }
}
else if (!logFilterCriteria.Checked1 && logFilterCriteria.Checked2)
{
if (DateTime.Compare(dtDateTime, logFilterCriteria.EndTime) > 0) { continue; }
}
else
{
if (DateTime.Compare(logFilterCriteria.StartTime, logFilterCriteria.EndTime)>0)
{
//MessageBox.Show("Start-time must be ahead of end-time!");
break;
}
if (DateTime.Compare(dtDateTime, logFilterCriteria.EndTime) > 0 ||
DateTime.Compare(dtDateTime, logFilterCriteria.StartTime) < 0)
{
continue;
}
}
}
this.BeginInvoke(updateTxt, nextLine);
//textBox1.AppendText(nextLine + "\r\n");
}
sr.Dispose();
}
fs.Dispose();
}
}
else
{
//MessageBox.Show("Not found log file");
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
|