Reputation: 31
I am trying to use CreateTimerQueueTimer(...)
to run a function every so often.
I am using an example from MSDN and mainly this line concerns me :
CreateTimerQueueTimer( &hTimer, hTimerQueue,(WAITORTIMERCALLBACK)TimerRoutine, &arg , 50,100, 0)
which the syntax is :
BOOL WINAPI CreateTimerQueueTimer(
_Out_ PHANDLE phNewTimer,
_In_opt_ HANDLE TimerQueue,
_In_ WAITORTIMERCALLBACK Callback,
_In_opt_ PVOID Parameter,
_In_ DWORD DueTime,
_In_ DWORD Period,
_In_ ULONG Flags
);
The second to last argument states
Period [in]
The period of the timer, in milliseconds. If this parameter is zero, the timer is signaled once. If this parameter is greater than zero, the timer is periodic. A periodic timer automatically reactivates each time the period elapses, until the timer is canceled.
As you can see in my code, I set the Due time for 50 and the period as 100. When I run it, it does not repeat
firing the timer. Can someone help me with this ?
Here is the entire code:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <thread>
#include <Windows.h>
#include <stdio.h>
using namespace std;
HANDLE gDoneEvent;
VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
if (lpParam == NULL)
{
printf("TimerRoutine lpParam is NULL\n");
}
else
{
// lpParam points to the argument; in this case it is an int
printf("Timer routine called. Parameter is %d.\n",
*(int*)lpParam);
if(TimerOrWaitFired)
{
printf("The wait timed out.\n");
}
else
{
printf("The wait event was signaled.\n");
}
}
SetEvent(gDoneEvent);
}
int main()
{
HANDLE hTimer = NULL;
HANDLE hTimerQueue = NULL;
int arg = 123,x;
// Use an event object to track the TimerRoutine execution
gDoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (NULL == gDoneEvent)
{
printf("CreateEvent failed (%d)\n", GetLastError());
return 1;
}
// Create the timer queue.
hTimerQueue = CreateTimerQueue();
if (NULL == hTimerQueue)
{
printf("CreateTimerQueue failed (%d)\n", GetLastError());
return 2;
}
// Set a timer to call the timer routine in 10 seconds.
if (!CreateTimerQueueTimer( &hTimer, hTimerQueue,(WAITORTIMERCALLBACK)TimerRoutine, &arg , 50,100, 0))
{
printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());
return 3;
}
// TODO: Do other useful work here
printf("Call timer routine in 10 seconds...\n");
// Wait for the timer-queue thread to complete using an event
// object. The thread will signal the event at that time.
if (WaitForSingleObject(gDoneEvent, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
CloseHandle(gDoneEvent);
// Delete all timers in the timer queue.
if (!DeleteTimerQueue(hTimerQueue))
printf("DeleteTimerQueue failed (%d)\n", GetLastError());
cin>>x;
return 0;
}
Thank you
Upvotes: 3
Views: 8243
Reputation: 503
@Lokesh is right, although as a c++ beginner I didn't get their answer at first.
You need to make sure SetEvent(gDoneEvent);
is called only when you want the timer to stop. For my needs I just commented it out as I wanted the timer to run continuously.
Upvotes: 0
Reputation: 21
Event gDoneEvent
is signaled before the function TimerRoutine()
exits. For repeated calls to the callback function, signal the event gDoneEvent
after the function TimerRoutine()
is called required number of times.
Could include the following lines of codes.
static int count = 0;
VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
......
count++;
if(count == 100)
{
SetEvent(gDoneEvent);
}
}
Upvotes: 1