Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I try this code:
C++
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>

#define TARGET_RESOLUTION 1
#define MAX_LOOP 5

HANDLE gDoneEvent;
int cnt = 0;
HANDLE hTimer = NULL;
HANDLE hTimerQueue = NULL;

VOID CALLBACK TimerRoutine(BOOLEAN TimerOrWaitFired)
{
    printf("Time Stamp : %u\n",__rdtsc());
    if(TimerOrWaitFired)
        printf("The wait timed out.\n");
    else
        printf("The wait event was signaled.\n");
	if(cnt++ >=MAX_LOOP)
		SetEvent(gDoneEvent);
}

void main (void)
{
	TIMECAPS tc;
	UINT     wTimerRes;

	gDoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (NULL == gDoneEvent)
    {
        printf("CreateEvent failed (%d)\n", GetLastError());
    }
	if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) 
	{
		printf("TimeGetDevCaps Error \n");
	}
	wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
	timeBeginPeriod(wTimerRes); 

	if (!CreateTimerQueueTimer( &hTimer, hTimerQueue, 
            (WAITORTIMERCALLBACK)TimerRoutine, NULL , 15, 1000, 0))
    {
        printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());
    }
	if (WaitForSingleObject(gDoneEvent, INFINITE) != WAIT_OBJECT_0)
		printf("WaitForSingleObject failed (%d)\n", GetLastError());

	CloseHandle(gDoneEvent);
	if (!DeleteTimerQueue(hTimerQueue))
			printf("DeleteTimerQueue failed (%d)\n", GetLastError());
}


My result seem correct as I expect, but I get an error code in the end line :
DeleteTimerQueue failed (6)

Following from MSDN's Document, they explain : The handle is invalid.

My questions are:
Why I get error code and How could I handle it?

Very thank you for every comments and guidelines
Posted

hTimerQueue is NULL, and I'm pretty certain you didn't mean to delete the default timer queue. Perhaps you really wanted to call DeleteTimerQueueTimer[^] on the timer hTimer that you created by the call to CreateTimerQueueTimer[^]?

Best regards
Espen Harlinn
 
Share this answer
 
Comments
windyl3ig 28-Mar-12 3:42am    
yes, exactly, you are right.
Espen Harlinn 28-Mar-12 4:07am    
Good :-D
windyl3ig 28-Mar-12 4:09am    
thank you very much
You didn't initialize the hTimerQueue variable. If you are intended to create a separate timer queue, then the code for it is missing.

The CreateTimerQueueTimer function takes the handle to the time queue, which is optional. And if you pass it as NULL (as you did) it associates the timer with default time queue.

There is no point in calling DeleteTimerQueue on hTimeQueue as you are not explicitly creating any.
 
Share this answer
 
Comments
windyl3ig 28-Mar-12 3:50am    
thank you for your guideline, I really miss it >w<</xml>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900