title: RichTextBox highlight performance
body: <p>I am working on application which highlight text in RichTextBox according to regex pattern. 
  It works fine, except performance, even for small text (about 500 characters) it's hangs for some time which is visible to user. </p>
  
  <p>Am I doing something wrong with FlowDocument? Can somebody point me out to the source of performance issue?</p>
  
  <pre><code>    public class RichTextBoxManager
  {
      private readonly FlowDocument inputDocument;
      private TextPointer currentPosition;
  
      public RichTextBoxManager(FlowDocument inputDocument)
      {
          if (inputDocument == null)
          {
              throw new ArgumentNullException("inputDocument");
          }
  
          this.inputDocument = inputDocument;
          this.currentPosition = inputDocument.ContentStart;
      }
  
      public TextPointer CurrentPosition
      {
          get { return currentPosition; }
          set
          {
              if (value == null)
              {
                  throw new ArgumentNullException("value");
              }
              if (value.CompareTo(inputDocument.ContentStart) &lt; 0 ||
                  value.CompareTo(inputDocument.ContentEnd) &gt; 0)
              {
                  throw new ArgumentOutOfRangeException("value");
              }
  
              currentPosition = value;
          }
      }
  
      public TextRange Highlight(string regex)
      {
          TextRange allDoc = new TextRange(inputDocument.ContentStart, inputDocument.ContentEnd);
          allDoc.ClearAllProperties();
          currentPosition = inputDocument.ContentStart;
  
          TextRange textRange = GetTextRangeFromPosition(ref currentPosition, regex);
          return textRange;
      }
  
      public TextRange GetTextRangeFromPosition(ref TextPointer position,
                                                string regex)
      {
          TextRange textRange = null;
          while (position != null)
          {
              if (position.CompareTo(inputDocument.ContentEnd) == 0)
              {
                  break;
              }
  
              if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
              {
                  String textRun = position.GetTextInRun(LogicalDirection.Forward);
                  var match = Regex.Match(textRun, regex);
                  if (match.Success)
                  {
                      position = position.GetPositionAtOffset(match.Index);
                      TextPointer nextPointer = position.GetPositionAtOffset(regex.Length);
                      textRange = new TextRange(position, nextPointer);
                      textRange.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Yellow);
                      position = nextPointer;
                  }
                  else
                  {
                      position = position.GetPositionAtOffset(textRun.Length);
                  }
              }
              else
              {
                  position = position.GetNextContextPosition(LogicalDirection.Forward);
              }
          }
  
          return textRange;
      }
  }
  </code></pre>
  
  <p>to call it, I first create an instance in Initialize method </p>
  
  <pre><code>frm = new RichTextBoxManager(richTextBox1.Document);
  </code></pre>
  
  <p>and on textchange event for textbox (where I put regex) I call Highlight method</p>
  
  <pre><code>frm.Highlight(textBox1.Text);
  </code></pre>