Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top