How to call a method every 2 hours in C#
Ho开发者_Go百科w would it be possible to call a method every 2 hours in C#
For example possibly make a click event occur every 2 hours
Timer Timer.Tick Event
- Add
Timer
component to your form. - Set interval to 7200000 ms (2 x 60 x 60 x 1000)
- Subscribe to
Tick
event. Call the method that you need to call. For example:
private void timer1_Tick(object sender, EventArgs e) { button1_Click(this, EventArgs.Empty); }
I assumed you are using Windows Forms.
If you are using WPF, there is DispatcherTimer
class.
Make an app which runs on a scheduled task if possible, it can then be set to call the method every 2 hours.
If your program uses forms, then a Timer can be added and configured to create a Timer.Tick Event every two hours (keep in mind Timer.Interval is in milliseconds). You can then use the Timer.Tick Event handler to call a method of your choice.
http://docs.castleproject.org/Tools.Scheduler.ashx
精彩评论