c++winapimsdnatlcom

Closing timers handle from the working thread


best way to ask a question is to first show an example:

this is how i create a timer in c++:

        if (FALSE == CreateTimerQueueTimer(&m_hSampleStarvationTimer,
                                            m_hSampleStarvationTimerQueue,
                                            (WAITORTIMERCALLBACK)TsSampleStarvationTimeBomb_Static,
                                            (LPVOID)this,
                                            dwDueTime,
                                            0,
                                            WT_EXECUTEONLYONCE))

once the following callback is triggered(TsSampleStarvationTimeBomb_Static), i try to kill both the queue handle and timer's handle inside that particular thread.

void CALLBACK CCaptureChannel::TsSampleStarvationTimeBomb_Static(LPVOID lpArg, BOOLEAN TimerOrWaitFired)
    {
        HRESULT hr;
        BOOL    bHandleDeletion          = FALSE;
        CCaptureChannel* pCaptureChannel = (CCaptureChannel*)lpArg;

        ATLASSERT(pCaptureChannel);

        bHandleDeletion = DeleteTimerQueueTimer(pCaptureChannel->m_hSampleStarvationTimerQueue, pCaptureChannel->m_hSampleStarvationTimer, NULL);
        bHandleDeletion = DeleteTimerQueue(pCaptureChannel->m_hSampleStarvationTimerQueue);

my question is: is it valid? i read over MSDN that the following deletion functions may return i/o errors which shouldn't concern me too much. their termination will be executed once the callback thread turns signled, automatically.

am i right? Thanks!


Solution

  • DeleteTimerQueueEx will cancel and delete all timers associated with the queue as soon as all timer callbacks complete, so a single call to DeleteTimerQueueEx will suffice. You don't need the call to DeleteTimerQueueTimer. If you call it from within the callback as you currently have in your code, you must pass NULL as the CompletionEvent parameter to avoid deadlocking.