I'm coming from Win32 world. Now, I'm porting the existing code to C#. I
cannot find the equivalent Win32 Waitable Timer in C#. I have a thread to
perform some task periodically like following:
While(...)
{
Do something ...
SetWaitableTimer(hTimer, ...);
WaitForSingleObject(hTimer, ...);
}
Could anybody tell me how to implement a waitable timer so a thread can
actually wait for it?
I don't know of any .NET class that duplicate the functionality exactly.
However, do you really need the APC behavior of the Win32 waitable timer
object? That is, is it important that the timer completion routine
execute on the same thread but without an explicit call in your code?
In .NET, you do have other timing options. System.Timers.Timer and
System.Threading.Timer both provide similar timing functionality, with the
main difference being that when the timer fires the delegate given to the
timer is run on another thread. If all you really want is to wait a
specific time, then you could just use a call to Sleep(). If you really
want to block while waiting for another thread to run the timer delegate,
you could wait on an event instance and have the timer delegate set the
event.
So, depending on what functionality you really need, you either can't do
it in .NET (AFAIK) or you can do it in slightly different ways. It seems
to me that in most cases, an application doesn't really need a Win32
waitable timer object, but you could be porting one of the rare blocks of
code that does. Knowing exactly what the timer's being used for would be
helpful in providing useful options for you.
Pete