Jeeva
Jeeva

Reputation: 4663

Scheduler using Timer Queues

I am working on an application where i need to schedule tasks based on the time set by the user. The user may add/modify/delete the schedules. To implement it i am considering using Timer Queues. Initially i though of using WaitableTimers which suite very much for my purpose but i cant make my thread to sleep for competing the APC.

Now with the Timer Queue i am not sure how to set the timer to signal based on Systemtime. I tried the following code but the callback function is never called

SYSTEMTIME st, lt;
GetSystemTime(&st); 

FILETIME ft;
SystemTimeToFileTime(&st, &ft);

ULONGLONG qwResult;

// Copy the time into a quadword.

qwResult = (((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;

// Add 20 seconds days.
qwResult += 20 * _SECOND;


HANDLE hTimerQueue = CreateTimerQueue();
HANDLE hTimer;

// Set a timer to call the timer routine in 10 seconds.

if (!CreateTimerQueueTimer( &hTimer, hTimerQueue ,(WAITORTIMERCALLBACK)TimerAPCProc, NULL , qwResult, 0, 0))

{

 printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());

 return 3;

}

Upvotes: 0

Views: 1978

Answers (3)

Cechner
Cechner

Reputation: 859

You're passing in an absolute time, but the docs say you need to pass in the number of milliseconds from the current time.

If you want the timer to go off in 20 seconds, pass 20000 instead of qwResult

Upvotes: 1

Djole
Djole

Reputation: 1145

The callback routine will be called in qwResult milliseconds, and file time gives you the time in 100 nanoseconds. You do the math. GetSystemTimeAsFileTime Will give you FILETIME right away if that is the path you want to go.

Personally, I would keep a list of structure with times when the routines should be called and pointers to routines and iterate through the list once in a while and if the time of execution is due I would just call the function (or create a thread). That way your users can always review the scheduled tasks and change them.

Upvotes: 2

Ajay
Ajay

Reputation: 18411

It needs to be backed by WaitForSingleObject, or entering the thread into waitable state (using SleepEx for example).

Upvotes: 1

Related Questions