472,119 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Generating an Event at a specific time

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#
Nov 15 '05 #1
2 15181
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.)

--
William Stacey, MVP

"dh****@ozemail.com.au" <an*******@discussions.microsoft.com> wrote in
message news:26****************************@phx.gbl...
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#


Nov 15 '05 #2
"dh****@ozemail.com.au" <an*******@discussions.microsoft.com> wrote in
message news:26****************************@phx.gbl...
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
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Andreas Lundgren | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.