views:

333

answers:

1

Hi,

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 500ms or 1000ms it will execute the complex processing runs.

Does this sound like a good idea?

+4  A: 

You should probably start your processing in it's 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 ... not sure if you can implement it this way, it depends on the type of your calculations.

tanascius
Nah, there's nothing useful that can be done in that thread. It cannot access any of the properties of the RTB.
Hans Passant
@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