I want my program to do a search on a database every few minutes to check for something specific and if it's found, then to show a certain form. I have 8 forms in my project and I want the search to be done while any of them is open. How do I program a timer to do that every few minutes?

The first time I want it done is 10 sec after the program is opened, where would I put the timer

Dani AI

Generated

Good move by to keep a single always-running component rather than duplicating timers across eight forms. Replies from , and pointed at putting a timer on a form or MDI — that is simple and works, but a few practical hardening tips will make it reliable in production.

Use one central poller (hidden form or a singleton class) that:

  • Does a very small query that detects changes only (for example check a last-modified timestamp or a monotonic ID) so the database work is cheap.
  • Opens the DB connection just long enough to run the check, then closes it.
  • Stores the last-seen timestamp/ID (in memory and/or persistent store) so restarts don’t reprocess old rows and time comparisons use UTC to avoid DST issues.

Make the poll safe and non-blocking:

  • Prevent overlapping runs by using a simple running-flag or disabling the poller while the check is in progress.
  • Run the database check off the UI thread (Task/BackgroundWorker or async DB APIs). If the check discovers something and a form must be shown, marshal that action back to the UI thread (Control.Invoke / SynchronizationContext).
  • Add error handling and an exponential backoff for transient DB failures, plus clean shutdown logic to stop the poller when the app exits.

If near-real-time notifications are required instead of polling, consider push mechanisms for your DB (for SQL Server, look into Query Notifications/SqlDependency) or move change-detection to a tiny notification table or message queue so the check is trivial.

These points keep the design simple (as suggested in the thread) while avoiding UI freezes, duplicated timers, missed changes, and connection leaks. For .NET timer details see the System.Timers.Timer documentation and for push alternatives see the SqlDependency overview.

Recommended Answers

All 4 Replies

Hi,

Place the Timer in any of the Form, and set its "Interval" 10000 (for 10 Seconds)

Write the Checking code in Timer1_Timer Event, Make the Event as Public and u can access it from every where.
If u have a MDI Form, then forst place a Frame Control and place the timer on that frame.

REgards
Veena

Hi,

Place the Timer in any of the Form, and set its "Interval" 10000 (for 10 Seconds)

Write the Checking code in Timer1_Timer Event, Make the Event as Public and u can access it from every where.
If u have a MDI Form, then forst place a Frame Control and place the timer on that frame.

REgards
Veena

Veena, I think he meant not Exactly a timer event. What he wants is when any of his form is activated he wants a search in the database for any change that has happened and report it in another form?. You better get the details. I am confident that he knows the timer event.

put the timer on the MDI forn using any container control and run the search code the timer_timer event at the interval you want.

I put the timer in a separate form which is open as long as the program is running so it's not dependend on which other form is open.
Thank you all for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.