Timer Question

jwcoleman87

Well-known member
Joined
Oct 4, 2014
Messages
124
Programming Experience
Beginner
In order to use a timer in my code, does my program need to run on a main loop?

I have an application that connects to an SQL server and updates and retrieves and queries information on a regular basis. It does not run on a loop, nor do the queries. I feel a "real-time" connection to the database could be a bit heavy for any network. The thing is. I want a timer in my main form, do I need to put my whole application into a main loop in order for it to update on a regular basis? I'm assuming yes. And in assuming yes I'm assuming a massive rework of my current code, which is all event driven at the moment.
 
You don't need to change anything. Add a Timer to your form, set its Interval and call its Start method. It will raise its Tick event on the specified interval. You handle the Tick event and you can then perform an action regularly.

Just note that the Tick event is raised on the UI thread, so you should not do anything lengthy in that Tick event handler. If you do need to do something lengthy then either do it asynchronously or else use a Timers.Timer, which can raise its Elapsed event on a secondary thread, instead of a Windows.Forms.Timer.
 

Latest posts

Back
Top