title: Highlighting rich text box with huge text efficiently in a windows form
body: <p>I have huge text files that I process line by line and add the results to a <code>StringBuilder</code> so I don't hold the main form with loading single lines of text to it. </p>
  
  <p>Once processing is done I dump the result to a <code>richtext textbox</code>. I want to highlight some of the text based on keywords I have. I end up using a string. Find on all the text for each word to highlight it. I tried having a thread to highlight the text with lambda expression richbox.BeginInvoke. the thread works fine, but takes the handle over the rich text box and is really slow. </p>
  
  <p>How do I loop through a <code>richtext box</code> line by line and highlight some words of it with understandable performance considering 50-100 MB of text?.</p>
  
  <p>This question has been moved from super user because its programming relevant.
  There are some proposed solutions such as :
  <a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=146" rel="nofollow">http://www.dotnetcurry.com/ShowArticle.aspx?ID=146</a>
  and
  <a href="http://www.codeproject.com/Articles/4031/Background-Highlighting-with-the-RichTextBox-the-S" rel="nofollow">http://www.codeproject.com/Articles/4031/Background-Highlighting-with-the-RichTextBox-the-S</a>
  , but they are still inefficient for large texts.</p>
  
  <pre><code>foreach (string x in LArgs)
  {
      int len =0;
      int index = 0;
      int lastIndex=0;
      output.Invoke(() =&gt; { len=output.Text.Length; });
      output.Invoke(() =&gt; { lastIndex=output.Text.LastIndexOf(x); });
      while (index &lt; lastIndex)
      {
          output.Invoke(() =&gt; { output.Find(x, index, len, RichTextBoxFinds.None); });
          output.Invoke(() =&gt; { this.output.SelectionBackColor = Color.Yellow; });
          output.Invoke(() =&gt; { index = this.output.Text.IndexOf(x, index) + 1; });
      }
  }
  </code></pre>
  
  <p>I added the library and used this code:</p>
  
  <pre><code>scintilla1.Text = output.Text;
  
  StringBuilder conf = new StringBuilder();
  conf.AppendLine("&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;");
  conf.AppendLine(@"&lt;ScintillaNET&gt;");
  conf.AppendLine("&lt;Language Name=\"log\"&gt;");
  conf.AppendLine("&lt;lexer LexerName=\"log\"&gt;");
  conf.AppendLine("&lt;Keywords List=\"0\"&gt;");
  
  foreach (string x in LArgs)
  {
      conf.Append(x + " ");
  }
  //var
  conf.AppendLine("&lt;/Keywords&gt;");
  conf.AppendLine(@"&lt;/lexer &gt;");
  conf.AppendLine(@"&lt;Styles&gt;");
  
  conf.AppendLine(@"&lt;/Language&gt;");
  conf.AppendLine(@"&lt;/ScintillaNET&gt;");
  
  File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory+@"ScintillaNET.xml", conf.ToString());
  scintilla1.Lexing.LexerLanguageMap["log"] = "cpp";
  scintilla1.ConfigurationManager.CustomLocation = AppDomain.CurrentDomain.BaseDirectory + @"ScintillaNET.xml";
  scintilla1.ConfigurationManager.Language = "log";
  scintilla1.ConfigurationManager.Configure();
  </code></pre>
  
  <p>The text is loaded but it does not highlight any text or any  text I add later on</p>