SetWaitableTimer function
Applies to: desktop apps only
Activates the specified waitable timer. When the due time arrives, the timer is signaled and the thread that set the timer calls the optional completion routine.
Syntax
BOOL WINAPI SetWaitableTimer( __in HANDLE hTimer, __in const LARGE_INTEGER *pDueTime, __in LONG lPeriod, __in_opt PTIMERAPCROUTINE pfnCompletionRoutine, __in_opt LPVOID lpArgToCompletionRoutine, __in BOOL fResume );
Parameters
- hTimer [in]
-
A handle to the timer object. The CreateWaitableTimer or OpenWaitableTimer function returns this handle.
The handle must have the TIMER_MODIFY_STATE access right. For more information, see Synchronization Object Security and Access Rights.
- pDueTime [in]
-
The time after which the state of the timer is to be set to signaled, in 100 nanosecond intervals. Use the format described by the FILETIME structure. Positive values indicate absolute time. Be sure to use a UTC-based absolute time, as the system uses UTC-based time internally. Negative values indicate relative time. The actual timer accuracy depends on the capability of your hardware. For more information about UTC-based time, see System Time.
- lPeriod [in]
-
The period of the timer, in milliseconds. If lPeriod is zero, the timer is signaled once. If lPeriod is greater than zero, the timer is periodic. A periodic timer automatically reactivates each time the period elapses, until the timer is canceled using the CancelWaitableTimer function or reset using SetWaitableTimer. If lPeriod is less than zero, the function fails.
- pfnCompletionRoutine [in, optional]
-
A pointer to an optional completion routine. The completion routine is application-defined function of type PTIMERAPCROUTINE to be executed when the timer is signaled. For more information on the timer callback function, see TimerAPCProc. For more information about APCs and thread pool threads, see Remarks.
- lpArgToCompletionRoutine [in, optional]
-
A pointer to a structure that is passed to the completion routine.
- fResume [in]
-
If this parameter is TRUE, restores a system in suspended power conservation mode when the timer state is set to signaled. Otherwise, the system is not restored. If the system does not support a restore, the call succeeds, but GetLastError returns ERROR_NOT_SUPPORTED.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
Timers are initially inactive. To activate a timer, call SetWaitableTimer. If the timer is already active when you call SetWaitableTimer, the timer is stopped, then it is reactivated. Stopping the timer in this manner does not set the timer state to signaled, so threads blocked in a wait operation on the timer remain blocked. However, it does cancel any pending completion routines.
When the specified due time arrives, the timer becomes inactive and the optional APC is queued to the thread that set the timer. The state of the timer is set to signaled, the timer is reactivated using the specified period, and the thread that set the timer calls the completion routine when it enters an alertable wait state. If the timer is set before the thread enters an alertable wait state, the APC is canceled. For more information, see QueueUserAPC. Note that APCs do not work as well as other signaling mechanisms for thread pool threads because the system controls the lifetime of thread pool threads, so it is possible for a thread to be terminated before the notification is delivered. Instead of using the pfnCompletionRoutine parameter or another APC-based signaling mechanism, use a waitable object such as a timer created with CreateThreadpoolTimer. For I/O, use an I/O completion object created with CreateThreadpoolIo or an hEvent-based OVERLAPPED structure where the event can be passed to the SetThreadpoolWait function.
If the thread that set the timer terminates and there is an associated completion routine, the timer is canceled. However, the state of the timer remains unchanged. If there is no completion routine, then terminating the thread has no effect on the timer.
When a manual-reset timer is set to the signaled state, it remains in this state until SetWaitableTimer is called to reset the timer. As a result, a periodic manual-reset timer is set to the signaled state when the initial due time arrives and remains signaled until it is reset. When a synchronization timer is set to the signaled state, it remains in this state until a thread completes a wait operation on the timer object.
If the system time is adjusted, the due time of any outstanding absolute timers is adjusted.
If the thread that called SetWaitableTimer exits, the timer is canceled. This stops the timer before it can be set to the signaled state and cancels outstanding APCs; it does not change the signaled state of the timer.
To compile an application that uses this function, define _WIN32_WINNT as 0x0400 or later. For more information, see Using the Windows Headers.
To use a timer to schedule an event for a window, use the SetTimer function.
Examples
For an example that uses SetWaitableTimer, see Using Waitable Timer Objects.
Requirements
|
Minimum supported client | Windows XP |
|---|---|
|
Minimum supported server | Windows Server 2003 |
|
Header |
|
|
Library |
|
|
DLL |
|
See also
- CancelWaitableTimer
- CreateWaitableTimer
- OpenWaitableTimer
- Synchronization Functions
- TimerAPCProc
- Waitable Timer Objects
Send comments about this topic to Microsoft
Build date: 3/7/2012
The most thorough examination of this API from a .NET perspective I've seen can be found at the link below:
http://www.devsource.com/c/a/Languages/Waitable-Timers-in-NET-CSharp/
Personally, I couldn't find the source code but it was enough of an introduction to begin getting good results in my own project. My main reason for commenting here though is that the due time works quite correctly as a count-down by simply using the value of the Ticks property of a TimeSpan.
However, an ordinary DateTime in the future doesn't seem to work in the way shown in Jim's article and that some extra MSDN documentation suggested that the API accepts a FILETIME struct.
Thus, the following code works in .NET as a pDueTime argument (tested on Vista):
long ticks = localTime.ToFileTimeUtc();
- 8/11/2009
- LukePuplett