Generating an Event at a specific time

  • Thread starter Thread starter dhourd
  • Start date Start date
D

dhourd

I'm performing a conversion of code from C to C# and I
want to perform a callback to a function where the
callback is performed at a certain time, like 2 March 2004
at 1:35pm.

I realise there are loads of timers in C#, but they all
perform repeatable events using an elapsed time which is
not what I desire.

The C code uses the functions
CreateWaitableTimer, SetWaitableTimer and
CancelWaitableTimer.

Is there an equivalent to this in C#
 
1) Can wrap them easy and use pinvoke.
2) Can take the time now and time in the future you want to hit and take the
difference (timespan) and use the Timer class (or other) to wait for that
TimeSpan amount, then in the callback method, cancel the timer and do your
stuff. You can also reset the timer to go off again at some other point in
the future if needed.
3) If your doing a scheduler, use the timer class to call a method that
enums a collection to see if some job's time has expired and run a
method/delegate associated with that job if so. Set timer delay to the
largest delay that will still give you the resolution you need (i.e. every
60 seconds, every 30 seconds, etc.)
 
I'm performing a conversion of code from C to C# and I
want to perform a callback to a function where the
callback is performed at a certain time, like 2 March 2004
at 1:35pm.

public Timer SetTimer(TimerCallback callback,object state,DateTime when)
{
TimeSpan diff=when-DateTime.Now;
if(diff<TimeSpan.Zero)
diff=TimeSpan.Zero;
return new Timer(callback,state,(int)diff.TotalMilliseconds,-1);
}

HTH
Mike
 
Back
Top