Reputation: 527
Can somebody give me a code, how to raise event when specified time is passed.
For example i have this times:
08:00 12:00 20:30 23:00
How can i subscribe to event which raising in these times?
Upvotes: 2
Views: 3440
Reputation: 3531
use a System.Timers.Timer
Details on this can be found at MSDN.
when the event goes off, raise the event AND reprogram the timer to go off again for your next event.
Upvotes: 0
Reputation: 6256
Try Quartz.Net.
It can run whatever you need in specified times, one of which is the firing of an event.
Upvotes: 0
Reputation: 3875
The method I used for an application was to make a timer that fires every few minutes. When it gets closer to the target time (e.g. current time + interval > target time), the interval becomes smaller.
For that particular application, exact precision wasn't that important, so I didn't let the interval get smaller than 500 ms.
Finally, when you think you're close enough, you do the task you need to at that time.
Upvotes: 0
Reputation: 100248
Try the following approach:
DateTime target = ...
int interval = (int)(target - DateTime.Now).TotalMilliseconds;
var timer = new System.Timers.Timer(interval);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
Upvotes: 3