tags:

views:

253

answers:

2

From MSDN:

The CancelWaitableTimer function does not change the signaled state of the timer. It stops the timer before it can be set to the signaled state and cancels outstanding APCs. Therefore, threads performing a wait operation on the timer remain waiting until they time out or the timer is reactivated and its state is set to signaled. If the timer is already in the signaled state, it remains in that state.

(Emphasis mine)

So: How do I set a manual-reset waitable timer to the non-signalled state, if I can't call CancelWaitableTimer? ResetEvent doesn't work on waitable timers (it fails with ERROR_INVALID_HANDLE).

I don't want to use SetWaitableTimer, because (at this point) I don't know what duration to set it to. I suppose I could set the delay to a large number, but that feels like a bit of a hack to me.

+1  A: 

Can you just call SetWaitableTimer with some very large time which isn't ever going to happen? And then call it again once you know the real time.

Update: *Hack or not, the docs are pretty clear that a m-r waitable timer "remains in the signalled state until SetWaitableTimer is called". Seems to me that that's the function you're going to have to use. You'll just have to have a nice constant called 'RESET_TIMER' which is set to -2^63 or something.*

Will Dean
I could, but that's a bit of a hack.
Roger Lipscombe
A: 

If you make the waitable timer auto-reset then it will reset automatically as soon as the wait for that timer completes.

However, from MSDN:

If multiple objects become signaled, the function returns the index of the first handle in the array whose object was signaled.

I originally thought that this means that you can lose a timer signal if an earlier handle in the wait array becomes signaled at the same time as the waitable timer, but I've since read that

WaitForMultipleObjects() scans the handle array from 0 onwards and returns as soon as it finds a signalled handle. Only that first found handle is reset to the unsignalled state; the others are untouched.

See Behavior of WaitForMultipleObjects when multiple handles signal at the same time

Ian Goldby