Hello everyone,
I try this code:
#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