Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Prevent RichTextBox Append which Can Cause OutOfMemory?

My Objective is keep logs line by line with RichtextBox control, But I am worry that when the lines reach to certain point , my window form will be hung or run of out memory..

Can any one show me how can i prevent this will happen, I have in mind maybe limit 300 lines with FIFO , or 500 lines then empty and refresh again..However i am not sure How Can i implement this.

    void WriteLog(string txt)
    {

        richTextBox1.AppendText(txt + Environment.NewLine);
        richTextBox1.HideSelection = false;
        richTextBox1.SelectionStart = richTextBox1.Text.Length;
    }
like image 207
Kelly Avatar asked Nov 05 '22 21:11

Kelly


1 Answers

If you want to delete lines than try to use this

    void WriteLog(string txt)
    {
        if(richTextBox1.Lines.Count() == 100)
        {
            DeleteLine(0);
        }
        richTextBox1.AppendText(txt + Environment.NewLine);
        richTextBox1.HideSelection = false;
        richTextBox1.SelectionStart = richTextBox1.Text.Length;
    }

    private void DeleteLine(int a_line)
    {
        int start_index = richTextBox1.GetFirstCharIndexFromLine(a_line);
        int count = richTextBox1.Lines[a_line].Length;

        // Eat new line chars
        if (a_line < richTextBox1.Lines.Length - 1)
        {
            count += richTextBox1.GetFirstCharIndexFromLine(a_line + 1) -
                ((start_index + count - 1) + 1);
        }

        richTextBox1.Text = richTextBox1.Text.Remove(start_index, count);
    }
like image 138
Stecya Avatar answered Nov 14 '22 02:11

Stecya