UI冻结和计算真的很慢

时间:2014-12-04 03:53:53

标签: c# multithreading user-interface freeze

我正在编写一个程序,它应该替换或删除logfile.txt中的一些条目。 代码工作正常(至少对于小型LogFiles)。如果我使用一个大文件(如27 MB),它变得非常慢,UI冻结。我无法点击任何东西。

按钮单击我执行此方法:

       private string delete_Lines(string[] lines, string searchString)
    {

        for (int i = 0; i < lines.Length; i++)
        {

            if (lines[i].Contains(searchString))
            {
                rtbLog.Text += "Deleting(row " + (i + 1) + "):\n" + lines[i] + "\n";
                progressBar1.Value += 1;
                if (cbDB == true)
                {
                    while (is_next_line_block(lines, i) == true)
                    {
                        i++;
                        rtbLog.Text += lines[i] + "\n";
                        progressBar1.Value += 1;
                    }
                }

            }
            else
            {
                res += lines[i]+"\n";
                progressBar1.Value += 1;
            }

        }
        tssLbl.Text = "Done!";
        rtbLog.Text += "...Deleting finished\n";
        return res;
    }

Lines是我正在尝试清理的日志文件的数组。每个条目都是一行。 tssLbl是一个通知标签,rtbLog是一个richTextBox,我跟踪我要删除的行。

is_next_line_block只是另一种方法,它检查下一行是我要删除的块的一部分。这种方法的参数是整行数组和行位置。

private bool is_next_line_block(string[] lines, int curIndex)
    {
        if (curIndex < lines.Length-1)
        {
            if (lines[curIndex + 1].StartsWith(" "))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }

    }

有任何人知道,导致冻结的原因是什么?我知道,我可以通过并行化来加速我的代码,但是我无法想象,检查一个没有并行性的27 MB txt文件需要很长时间。

3 个答案:

答案 0 :(得分:0)

这里有几个问题:

  1. 你正在缓冲区(字符串数组)中读取整个文件,我猜你正在调用File.ReadAllLines()。在缓冲区中读取大文件会降低你的速度,并且在极端情况下会让你内存不足。

  2. 您对富文本框Text属性使用+ =操作。这是一项耗时的操作,因为每次以这种方式更新文本属性时,UI都必须呈现整个富文本框。更好的选择是使用字符串构建器来加载这些文本,并定期更新富文本框。

  3. 要解决此问题,您需要将文件作为流读取。可以基于读取的字节而不是行位置来监视进度。您可以在计时器上运行读操作异步并监视进度,如下例所示。

    private void RunFileOperation(string inputFile, string search)
    {
        Timer t = new Timer();
        int progress = 0;
        StringBuilder sb = new StringBuilder();
    
        // Filesize serves as max value to check progress
        progressBar1.Maximum = (int)(new FileInfo(inputFile).Length);
        t.Tick += (s, e) =>
            {
                rtbLog.Text = sb.ToString();
                progressBar1.Value = progress;
                if (progress == progressBar1.Maximum)
                {
                    t.Enabled = false;
                    tssLbl.Text = "done";
                }
            };
        //update every 0.5 second       
        t.Interval = 500;
        t.Enabled = true;
        // Start async file read operation
        System.Threading.Tasks.Task.Factory.StartNew(() => delete_Lines(inputFile, search, ref progress, ref sb));      
    }
    
    private void delete_Lines(string fileName, string searchString, ref int progress, ref StringBuilder sb)
    {
        using (var file = File.OpenText(fileName))
        {
            int i = 0;
            while (!file.EndOfStream)
            {
                var line = file.ReadLine();
                progress = (int)file.BaseStream.Position;
                if (line.Contains(searchString))
                {
                    sb.AppendFormat("Deleting(row {0}):\n{1}", (i + 1), line);
                    // Change this algorithm for nextline check
                    // Do this when it is next line, i.e. in this line.
                    // "If" check above can check if (line.startswith(" "))...
                    // instead of having to do it nextline next here.
                    /*if (cbDB == true)
                    {
                        while (is_next_line_block(lines, i) == true)
                        {
                            i++;
                            rtbLog.Text += lines[i] + "\n";
                            progressBar1.Value += 1;
                        }
                    }*/
                }
            }
        }           
        sb.AppendLine("...Deleting finished\n");
    }
    

答案 1 :(得分:0)

作为关于Task.Factory.Start()用法的问题的后续跟进,它是以这种方式完成的(通常):

// you might need to wrap this in a Dispatcher.BeginInvoke (see below)
// if you are not calling from the main UI thread
CallSomeMethodToSetVisualCuesIfYouHaveOne();

Task.Factory.StartNew(() =>
{
    // code in this block will run in a background thread...
}
.ContinueWith(task =>
{
   // if you called the task from the UI thread, you're probably
   // ok if you decide not to wrap the optional method call below
   // in a dispatcher begininvoke... 
   Application.Current.Dispatcher.BeginInvoke(new Action(()=>
   {
       CallSomeMethodToUnsetYourVisualCuesIfYouHaveAnyLOL();
   }));
}

希望这有帮助!

答案 2 :(得分:0)

感谢大家的帮助,特别是循环代码,这是工作版本(Took循环代码的代码并进行了一些编辑):

        private void RunFileOperation(string inputFile, string search)
    {
        Timer t = new Timer();
        StringBuilder sb = new StringBuilder();
        {
            rtbLog.Text = "Start Deleting...\n";
        }


        // Filesize serves as max value to check progress
        progressBar1.Maximum = (int)(new FileInfo(inputFile).Length);
        t.Tick += (s, e) =>
        {
            rtbLog.Text += sb.ToString();
            progressBar1.Value = progress;
            if (progress == progressBar1.Maximum)
            {
                t.Enabled = false;
                tssLbl.Text = "done";
            }
        };
        //update every 0.5 second       
        t.Interval = 500;
        t.Enabled = true;
        // Start async file read operation
        if (rbtnDelete.Checked)
        {
            if (cbDelete.Checked)
            {
                System.Threading.Tasks.Task.Factory.StartNew(() => delete_Lines(inputFile, search, ref progress, ref sb, ref res1));
            }
    }
    else 
    {
       //..do something
    }

    private void delete_Lines(string fileName, string searchString, ref int progress, ref      StringBuilder sb, ref StringBuilder res1)
    {
    bool checkNextLine=false;
        using (var file = File.OpenText(fileName))
        {
            int i = 0;
            while (!file.EndOfStream)
            {
                i++;
                var line = file.ReadLine();
                progress = (int)file.BaseStream.Position;
                if (line.Contains(searchString))
                {
                    sb.AppendFormat("Deleting(row {0}):\n{1}\n", (i), line);
                    checkNextLine = true;
                }
                else
                {
                    if (cbDB && checkNextLine && line.StartsWith(" "))
                    {
                        sb.AppendFormat("{0}\n", line);
                    }
                    else
                    {
                        checkNextLine = false;
                        res1.AppendLine(line);

                    }
                }

            }
        }
        sb.AppendLine("\n...Deleting finished!);
    }