2

Question

Is it possible to speed up the colorizing of lines in a RichtextBox?

Situation

I have log files where the lines with errors must be shown colorized. For this I use the RichttextBox control which has a very poor perfomance.

What I reached until now

I did already this to speed it up:

After that I need 11 seconds for colorizing 100 lines of 2000 lines instad of 40 seconds before.

Other Questions

There are several other questions like the mentioned Richedit50W about speeding RichtextBox up or colorizing. But no answer helps with speeding up colorizing.

My code

Can I do anything more to speed it up?

private void ColorizeLog(List<int> ErrorLines)
{
    // The List "ErrorLines" is filled with the wanted line numbers
    // and was build before filling the RichtextBow with the log data


    // Counting the lines before the for..next is 50% faster than:
    //      for (int lineNo = 0; lineNo < this.richTextbox50_Log.Lines.Count(); lineNo++)   
    //      in combination the other usage of "countLines" deeper in the code
    int countLines = this.richTextbox50_Log.Lines.Count();
    for (int lineNo = 0; lineNo < countLines; lineNo++)
    {

        if (ErrorLines.IndexOf(lineNo) >= 0)
        {
            int pos1 = this.richTextbox50_Log.GetFirstCharIndexFromLine(lineNo);
            int pos2 = lineNo < countLines  - 1 ?
                        this.richTextbox50_Log.GetFirstCharIndexFromLine(lineNo + 1) - 1 :
                        this.richTextbox50_Log.Text.Length;

            this.richTextbox50_Log.Select(pos1, pos2 - pos1);
            this.richTextbox50_Log.SelectionColor = Color.White;
            this.richTextbox50_Log.SelectionBackColor = Color.Red;
        }
}
marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52
  • This might belong on [Code Review](http://codereview.stackexchange.com/) instead. – DonBoitnott Jun 07 '16 at 11:37
  • If the code works but is just slow, that would be OK for Code Review – Phrancis Jun 07 '16 at 11:38
  • If you have lines before you add them to the richtextbox you may do better to add lines then set a colour, add the next line, revert the colour, add more lines. – BugFinder Jun 07 '16 at 11:40
  • Try http://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting or http://stackoverflow.com/questions/16703901/fastest-way-to-append-text-to-a-richtextbox – Murray Foxcroft Jun 07 '16 at 11:42
  • I'd use list control (e.g. `ListView`) with virtualization enabled for this kind of job (to display and do something with thousands of items). – Sinatr Jun 07 '16 at 11:45
  • 4
    It is never going to be particularly fast. But it is important to stop the RTB from repainting itself whenever you colorize *one* line, you want to delay that until you're done with all of them. It is missing the Begin/EndUpdate() methods, they are however [very easy to add](http://stackoverflow.com/a/3282911/17034). – Hans Passant Jun 07 '16 at 11:46
  • @HansPassant I added your code to my already derived class but it shows no effect. Neither in the Richedit50 nor in the Richedit20 version. – marsh-wiggle Jun 07 '16 at 12:04
  • @bugfinder this makes it really about 30% faster but its still very slow. For logifles with > 200,000 lines it takes about 1.5 minutes to load. – marsh-wiggle Jun 07 '16 at 12:25
  • @sinatr sounds intersting. the logs may have > 200.000 lines. Do you know anything about the memory consumption? – marsh-wiggle Jun 07 '16 at 12:26
  • @MurrayFoxcroft The most promissing answer. But our customer doesn't allow components under LGPL and similar licenses. – marsh-wiggle Jun 07 '16 at 12:30
  • @boboes, memory consumption of virtualized listbox? It's great (only visible on screen items will be presented as `ListBoxItem`, the rest take 0 memory in UI terms). – Sinatr Jun 07 '16 at 12:30
  • @Sinatr I'll check it in the next days and tell you about the results. Thanks :) – marsh-wiggle Jun 07 '16 at 12:32
  • 2
    If you see no effect at all then you are doing it wrong. But jeez marie, 200,000 lines. What kind of user is going to enjoy reading 4 copies of War and Peace through your user interface?? Look at the way Google presents search hits for common words with a million matches. – Hans Passant Jun 07 '16 at 13:03
  • @boboes, contact the component owner - his email is in the license and see if he will give you a better license - MIT / Apache etc. – Murray Foxcroft Jun 07 '16 at 13:05
  • @HansPassant If you agree I'll write a little demo code an post it as another question. And, yes, you are right. But that's what the customer wants. A solution with integrated log-viewer with highlighting, search and analyzing functions written in C#. The log viewer is just for the logs the solution generates. – marsh-wiggle Jun 07 '16 at 13:11
  • Then give him what he wants but not in such a straight forward brute-force way. Intelligent paging is called for. Intelligent meaning that system and customer are both fooled into being happy. Yes, that leaves extra chores to you. Part of the job.. – TaW Jun 07 '16 at 13:28
  • @TaW Right. I'll take a look at the virtualized ListBox. – marsh-wiggle Jun 07 '16 at 13:43
  • If you need rapid colorizing of lines, and the ability to support ginormous swaths of text, perhaps you should use a control designed for explicitly for that purpose? Something like [Scintilla](http://www.scintilla.org/), perhaps? With that much text, it still won't perform instantaneously, but it's about as optimal as you're going to get. Almost certainly faster than trying to manually tweak a RichEdit control. You can link to it either as a DLL deployed alongside your app, or build it as a static library so as to take no dependencies. – Cody Gray - on strike Jun 07 '16 at 14:12

1 Answers1

1

For one of my projects, I have felt the need of a text editor with syntax highlighting. At first, I used a component inherited from RichTextBox, but while using it for a large amount of text I found out that RichTextBox highlights very slowly a large number of colored fragments (from 200 and more). When such highlighting has to be made in a dynamic way, it causes a serious problem.

http://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting

I reckon RichTextBox is just a b*** at times...

Leon
  • 919
  • 6
  • 21