2

I have a strange issue that I'm not too sure on how to fix or address. I'm writing a mini text editor style application - RichTextBox editor.

I need to do some complex parsing after the selection changes - updating position, selection text and various other bits about the context of the text around the area.

As it takes a bit of processing I don't want it to fire each time the selection changes if the user is scrolling with their arrow keys. I thought of using the Application.Idle, but it fires too regularly. I tried a timer, but it may fire while the selection arrows are still moving.

What I was thinking of was a countdown timer sort of utility that will reset the timer each time the RichTextBox SelectionChanged event fires, then when the timer hits 500 ms or 1000 ms it will execute the complex processing runs.

Does this sound like a good idea?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • when do you have to do this processing? When something is selected or even when the cursor position changes only (SelectionLength == 0)? – tanascius Jun 10 '10 at 11:54

1 Answers1

4

You should probably start your processing in its own thread when it takes too long. As soon as you get new inputs you can stop the previous calculation and start with the new information again (so consider a cancel mechanism for your thread).

When your thread is done you have to check if its results are valid (the selecion did not change in the meantime). Finally, you can "synchronize" the results of the calculation to the GUI, which is hopefully quick enough :)

This does only work, when there is a certain amount of calculation that can be done without writing to the GUI ... I am not sure if you can implement it this way. It depends on the type of your calculations.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tanascius
  • 53,078
  • 22
  • 114
  • 136
  • 1
    Nah, there's nothing useful that can be done in that thread. It cannot access any of the properties of the RTB. – Hans Passant Jun 10 '10 at 12:57
  • @Hans ... that's why I wrote that it is depending on the type of the calculation ... doing e.g. a spellcheck can be done in a thread ... coloring the wrong words has to be done outside, of course – tanascius Jun 10 '10 at 13:14