I would like to know how to programmatically call a mathod regarding on time. In my example I would like to call a method every hour.

Can I use a Timer or is there something else?

8 Years
Discussion Span
Last Post by mcriscolo

If it's a Windows Forms app, yes, you can use the Timer object and set the "Interval" property to the desired value, in milliseconds, then call the "Start()" method to start the timer. It will fire the "Tick" method when the interval is reached. From there, you can call any method you like.

If you want to do it all in code, you can use the System.Threading.Timer object. Plenty of examples on the web of how to use that facility, but generally:

Timer myTimer;

// myTimerCallBack - method to call
// someData - blob of data that will get passed to the callback (can be null)
// timeBeforeFirstExec (in milliseconds)  - time before the first time the timer fires
// period (in milliseconds) - recurring interval to fire the timer
myTimer = new Timer(myTimerCallback, someData, timeBeforeFirstExec, period);

public void myTimerCallback(Object obj)
    // obj is the "someData" from the call, above
    // Do timer stuff

Hope this helps.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.