I am creating a simple log parsing/reading application which becomes unresponsive when attempting to highlight a matched string (user defined input) and reports log file line string was found on within richTextBox1
for a large text file (1.5Mb or larger). Small text files work fine. Matched lines seem be displayed fine. The problem exists when attempting to highlight the matched string (when matched string is increasingly found).
//application has user select *.txt file and assign contents to string[] logContents, not streaming file.
//list of matched strings is made
//matched strings are displayed in richTextBox1
StringBuilder RTBtext = new StringBuilder(richTextBox1.Text);
if (displayMatched != null && displayMatched.Length > 0)
{
//displays line of log file that matched the entered string
for (int s = 0; s < displayMatched.Length; s++)
{
RTBtext.Append(displayMatched[s] + Environment.NewLine);
}
}
richTextBox1.Text = RTBtext.ToString();
//newline \n added between matched line to increase readability
//Performance debugging shows "hot lines" with large CPU usage start here:
while (index < richTextBox1.Text.ToUpper().LastIndexOf(this.userInput1))
{
richTextBox1.Find(this.userInput1, index, richTextBox1.Text.Length,RichTextBoxFinds.None);
richTextBox1.SelectionColor = Color.White;
richTextBox1.SelectionBackColor = Color.Blue;
index = richTextBox1.Text.ToUpper().IndexOf(this.userInput1, index) + 1;
}
A similar style loop in another part of the program executes without issue. Do I have a problem with indexing? What is making the CPU work so hard? Performance debugging states System.Windows.Forms.RichTextBox.get_Text();
is the biggest bottleneck.
I have tried richTextBox1.SuspendLayout
and ResumeLayout
with no luck (also RTB1.SuspendPainting
/ResumePainting
).
Is it possible to only highlight, say, about 200 lines of matched strings at one time, then continue with richTextBox1.VScrollBar
? If so, what is the best way to accomplish this?