0

我从文本文件加载大量数据并将其显示在 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);
            }
        }
    }
4

1 回答 1

0

我在您的代码中没有看到 UI 更新的任何地方,所以我假设导致缓慢的原因是从磁盘读取数据。您可以尝试将线程从磁盘读取数据的优先级设置为低于 Normal 的值,以便 UI 线程有更好的 CPU 周期机会。请参阅线程优先级属性

此外,如果文件中行的长度不大,并且鉴于读取文件的代码已经在后台线程中运行,我将只使用ReadLine而不是使用ReadLineAsync并将工作传递给另一个线程。

于 2013-01-31T21:58:24.323 回答