我从文本文件加载大量数据并将其显示在 Datatgrid 中,问题是窗口很慢而且不流畅,我怎样才能更好地实现下面的代码?
按钮代码:
private async void MILoadLogFile_Click(object sender, RoutedEventArgs e) {
// Configure open file dialog box
OpenFileDialog oFD = new OpenFileDialog();
// Did they click on the OK button?
if (oFD.ShowDialog() == true) {
await myLogSession.LoadfromFileAsync(oFD.FileName);
}
}
locad 方法:(抱歉代码太长)
public async Task LoadfromFileAsync(String fileName) {
compassLogCollection.Clear();
StreamReader streamReader = new StreamReader(fileName);
if (fileName.Contains("Compass")) {
String temp = "";
String line;
DateTime dateTime = new DateTime();
LoggingLvl loggingLvl = new LoggingLvl();
LoggingLvl.ELoggingLvl eLoggingLvl = new LoggingLvl.ELoggingLvl();
char[] delimiters = new[] {' '};
string threadId = "";
string loggingMessage;
string dateAndTimestamp = "";
int ff = 0;
try {
using (streamReader) {
while ((line = await streamReader.ReadLineAsync()) != null) {
//while ((line = streamReader.ReadLine()) != null) {
string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
foreach (string t in parts) {
switch (ff) {
case 0:
dateAndTimestamp = t;
break;
case 1:
dateAndTimestamp += " " + t.Replace(",", ".");
dateTime = DateTime.Parse(dateAndTimestamp);
dateAndTimestamp = "";
break;
case 2:
eLoggingLvl = loggingLvl.ParseLoggingLvl(t);
break;
case 3:
threadId = t;
break;
default:
temp += t;
break;
}
ff++;
}
loggingMessage = temp;
temp = "";
ff = 0;
loggingLvl = new LoggingLvl(eLoggingLvl);
CompassLogData cLD = new CompassLogData(dateTime, loggingLvl, threadId, loggingMessage);
compassLogCollection.Add(cLD);
}
Console.Out.WriteLine("DOOOOOOOOOOOOONE");
}
} catch (Exception e) {
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}