开发者

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;
S开发者_JAVA技巧ystemTimeToFileTime(&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;

}


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜