How to run code after one second of inactivity (from a textbox)?

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
Hi, could someone help me to make some code run after one second of inactivity of a textbox.

In more detail, say I have a text box, I want there to be a method (AddToHistory) called every second, if there is any activity in the text box the one-second timer is reset, once the the second has passed and the AddToHistory method called the timer should stop until activity is detected again in the textbox (to prevent the AddToHistory method from being pointlessly called non-stop).

Could someone give me an idea how to do this please?

Thank you
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,506
Location
Sydney, Australia
Programming Experience
10+
Add a Timer to your form, set its Interval property to 1000 and handle its Tick event. In the event handler, call Stop on the Timer and then do whatever it is that you want to do. Handle the TextChanged event of your TextBox and call Stop and Start on your Timer.
 
Top Bottom