Tutorial :Running repetative method at specified moments (windows, .net etc) (like clock)



Question:

This is not an urgent nor important question, seems more like exercise.

How to run a function at a specific moment? The more precise, the better.

For instance, I have a method which says time. I want to run it on XX o'clock, XX:15:00 (and preferably, 000 ms), XX:30:00.000, XX:45:00.000.

Currently I have an (almost endless) loop, which checks DateTime.Now, calculates the TimeSpan to sleep for, sleeps for that inverval â€" 30 s (if that interval is bigger), sleeps again the same way, then calls the function.

The side-effect of this approach is stand-by mode. It seems that OS does not decrease the amount of sleeping time after waking up, so that when my laptop goes standby at XX:01, wakes up at XX:13, the thread will still be asleep, and therefore miss the XX:15:00.000 moment.

Maybe there is an easier way to say "call this function at XX:00:00.000" precisely.


Solution:1

The Quartz.Net library seems what you're looking for. It is a very flexible, open source job scheduling library, with good tutorials. It seems to be actively maintained as well (latest release from May 2009)

With this library, you can schedule jobs with all kinds of scheduling patterns, among which "at a certain time of day (to the millisecond)" (see features of quartz.net)

I'm not sure whether it copes with the standby event however...


Solution:2

You could try capturing the Windows sleep mode event, so that when Windows goes into that mode, you can set a variable to the current time. Then when Windows wakes up, use that variable to calculate how many milliseconds passed so that you can sync your timer to the correct time. Check out this link, it might help you:


Solution:3

I would simply sleep for a smaller interval (say 10 seconds, whatever the granularity you want is) and determine whether the next action in your queue is scheduled to be run (and run it). Depending on how efficient your checking code is you'll probably find that it takes a remarkably trivial amount of CPU to do that.

You would also handle the other edge cases of the clock changing (being reset by the user/time zone etc.) which gets rather more complex than just handling a sleep/wakeup event.


Solution:4

if this does not need to be coded, you might use the builtin task execution facility of windows, you find it in control panel. don't know anymore what the exact title is called... "Scheduled Tasks" or kind of... you may specify an executable, a point in time an if i'm right you can specify as well some kind of repetition.

i'm not sure at all about how exact this method fires the events...

when i had to implement something in that kind, i did an approach using System.Timers.Timer and a TimeSpan (to be able to define the resolution). this method was never very precise, but it wasn't required as well. to get a better result, maybe on startup you synchronize this timer to a "round" number with the current system clock and then, let's say, set an interval to tick each 15 minutes...

regards


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »