Coder Perfect

How should a “timer” be implemented? [duplicate]


What is the most effective approach to use a timer? It would be fantastic if you could provide a code sample. In this case, “best” means most dependable (lowest number of misfires) and exact. I want the target method to be called every 15 seconds, not every 10 – 20 seconds, if I give a 15-second period. I, on the other hand, do not require nanosecond precision. In this case, firing the method every 14.51 – 15.49 seconds would be okay.

Asked by Robert

Solution #1

Use the Timer class to keep track of time.

public static void Main()
    System.Timers.Timer aTimer = new System.Timers.Timer();
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    aTimer.Interval = 5000;
    aTimer.Enabled = true;

    Console.WriteLine("Press \'q\' to quit the sample.");
    while(Console.Read() != 'q');

 // Specify what you want to happen when the Elapsed event is raised.
 private static void OnTimedEvent(object source, ElapsedEventArgs e)
     Console.WriteLine("Hello World!");

The Elapsed event will be raised every X milliseconds, as provided by the Timer object’s Interval parameter. The Event Handler method you define will be called. It’s OnTimedEvent in the example above.

Answered by Dave Zych

Solution #2

You can achieve your goal by using the System.Windows.Forms.Timer class.

System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();

t.Interval = 15000; // specify interval time as you want
t.Tick += new EventHandler(timer_Tick);

void timer_Tick(object sender, EventArgs e)
      //Call method

Stop the timer using the stop() method.


Answered by Jignesh Thakker

Solution #3

It’s unclear what kind of application you’ll be creating (desktop, web, console…)

If you’re working on a Windows.Forms application, the standard answer is to utilize

System.Windows. Forms. Timer is a type of class. Because it runs on the UI thread, it’s as simple as defining it, subscribing to its Tick event, and running your code every 15 seconds.

You can choose System if you’re doing something other than windows forms (it’s not evident from the question). Timers. Timer, but this one runs on a different thread, so you’ll have to handle it using “invoking” access if you want to act on some UI components from its Elapsed event.

Answered by Tigran

Solution #4

Put the following code in the OnStartevent of your class, referencing ServiceBase:

Constants. 1 for TimeIntervalValue (hour).. This value should ideally be specified in the config file.

StartSendingMails = the name of the application’s function.

 protected override void OnStart(string[] args)
            // It tells in what interval the service will run each time.
            Int32 timeInterval = Int32.Parse(Constants.TimeIntervalValue) * 60 * 60 * 1000;
            TimerCallback timerDelegate = new TimerCallback(StartSendingMails);
            serviceTimer = new Timer(timerDelegate, null, 0, Convert.ToInt32(timeInterval));

Answered by A Developer

Post is based on