Inplement a precise timer
-
I'm not aware of any explicit support for a due time. For a 1-second resolution I would just pick some timer (Windows.Forms/Threading/Timers depending on circumstances) and use DateTime and TimeSpan classes to calculate and set the delay. For a finer resolution, I'd start with the above, then probably execute some polling loop within the final second, including a Thread.Sleep(ms) with a number of milliseconds fitting the resolution (assuming >= 50) Whatever you do, keep in mind things could go wrong in several ways: - your PC might get very busy while calculating and setting the due time, resulting in a late call; - your PC might get very busy at the due time, so the requested action may actually run somewhat later; - your PC might be switched off or crash in the mean time. Depending on your requirements a Windows scheduled task may or may not be a better option. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
quartz.net has a number of timers, a cron type timer being amongst them - may be worth a look 'g'
thanks, i'll take a look at quartz.net.
-
I think you're right on the life-critical aspect. I don't believe there is anything in .net that will give you that granularity. I don't know if my idea fits your needs, or is even a step down the right path. In the solution I was talking about, we knew every time an action was taken exactly what the next 'automatic' action would be, and exactly when it would occur. However, for us, not so time-critical. So, even though we were able to come up with a simple 'if this happens, write this event and time to the database' scenario, our server wouldn't necessarily notice that event right at the time written. But, it did turn out to be a really simple solution to a timer-type problem. I suppose in today's world, you'd just go ahead and use a Timer and tie it to the event you wanted to perform. Anyway, good luck.
FWIW: In a Windows Service that I wrote awhile back, I used the following: // Initiate our System Timer (set to one minute here) double dblStartupInterval = 60000; this.timProcessTimer_PollPV = new System.Timers.Timer (dblStartupInterval); // Note that everything done by the Timer is tied to // the Elapsed Event, which is raised every interval. // Instruct the Framework to call // the "ServiceTimer_PollPV_Tick" method when this // Timer elapses -- that is, when its interval runs its course. this.timProcessTimer_PollPV.Elapsed += new System.Timers.ElapsedEventHandler(this.ServiceTimer_PollPV_Tick);