简体   繁体   中英

Make Wpf more Smooth

I load a lage data fra a text File and display it in a Datatgrid, the problem is that the windows is slow and not smootth, how can i implemented the code below better?

the button Code:

        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);
        }
    }

the locad method:(sorry for long Code)

        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);
            }
        }
    }

I don't see anywhere in your code where UI is updated, so I'm assuming what's causing the slowness is reading of the data from the disk. You can try setting the priority of the thread reading the data from the disk to something lower than Normal so that the UI thread has a better chance at CPU cycles. See the thread priority property .

Also, if the length of the lines in your file are not large and given that the code reading the file is already running in a background thread, I would just use ReadLine instead of using ReadLineAsync and passing the work to yet another thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM