Is there an object that will event at a given DateTime or DateTimeOffset? I am using a Timer to do this operation now but it requires a bit of calculation on my part, was just thinking there might already be something in place.
A:
If you are using ASP.NET you may be able to add an empty object into the Cache with a declared ExpirationDate then handle the CacheItemRemovedCallback. Just a thought, don't know if it will work for you.
If you not using ASP.NET you still may be able to use this trick with another caching framework such as the Enterprise Library Caching Block
bendewey
2009-03-22 05:10:35
Yeah, I think that's more complicated/hacky than just using a timer and calculating the interval :) Nice thought though.
spoon16
2009-03-22 05:56:25
@spoon16, agreed its not a technique I've ever used, or needed to, but I heard of it somewhere.
bendewey
2009-03-22 14:17:06
+2
A:
I don't see how you have to do any calculation:
public void StartTimer(DateTime target) {
double msec = (target - DateTime.Now).TotalMilliseconds;
if (msec <= 0 || msec > int.MaxValue) throw new ArgumentOutOfRangeException();
timer1.Interval = (int)msec;
timer1.Enabled = true;
}
Hans Passant
2009-03-22 06:06:46
+1
A:
I like:
System.Timers.Timer _WaitForScheduledTime;
_WaitForScheduledTime = new System.Timers.Timer();
_WaitForScheduledTime.Elapsed += new ElapsedEventHandler(WaitForScheduledTime_OnElapsed);
_WaitForScheduledTime.Interval = _ListOfJobs.IntervalUntilFirstJobIsToRun().TotalMilliseconds;
_WaitForScheduledTime.Start();
...
private void WaitForScheduledTime_OnElapsed(object source, EventArgs e)
{
log.Debug("Ready to run at least one job");
// restart the timer
_WaitForScheduledTime.Interval = _ListOfJobs.IntervalUntilFirstJobIsToRun().TotalMilliseconds;
_WaitForScheduledTime.Start();
}