One approach:
-
Create a
Timer
with anInterval
of X millisecondsThe interval should be about 300ms; more than a normal time between keystrokes, and also reasonable time to wait between finishing and the update occurring
-
In the input’s
TextChanged
event,Stop()
and thenStart()
theTimer
This will restart the
Timer
if it is already running, so if the user keeps typing at a normal rate, each change will restart the timer. -
In the timer’s
Tick
event,Stop()
theTimer
and do the long transaction -
Optional: Handle the
Leave
andKeyDown
events so that leaving the control or pressing Enter willStop()
theTimer
and do the long transaction.
This will cause an update if the text has changed, and the user hasn’t made any changes in X milliseconds.
One problem with the “Measure the time since the last update” approach you’re considering is that if the last change is made quickly, the update won’t happen, and there won’t be any subsequent changes to trigger another check.
Note: There must be a one to one pairing between TextBox
es and Timer
s; if you’re planning on doing this with more than one input, I’d consider building a UserControl
that wraps this functionality.