【问题标题】:Make Wpf more Smooth使 Wpf 更流畅
【发布时间】:2013-02-01 05:58:24
【问题描述】:

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

【问题讨论】:

  • 显示绑定到 UI 的位置

标签: c# wpf multithreading asynchronous async-await


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 2017-03-02
相关资源
最近更新 更多