#include <windows.h>
#include <stdio.h>
TASK (Task2ms)
{
printf("Hello"):
SetEvent(Task1);
}
void main()
{
int arg;
HANDLE Task1;
HANDLE HTimer1 =NULL;
HANDLE HTimerQueue1 = NULL;
Task1 = CreateEvent(NULL, TRUE, FALSE, NULL);
if(NULL == Task1)
{
printf("CreateEvent failed (%d)\n", GetLastError());
return 1;
}
//create a timer queue
HTimerQueue1 = CreateTimerQueue();
if(NULL == HTimerQueue1)
{
printf("CreateTimerQueue failed (%d)\n", GetLastError());
return 2;
}
//phNewTimer - Pointer to a handle; this is an out value
//TimerQueue - Timer queue handle. For the default timer queue, NULL
//Callback - Pointer to the callback function
//Parameter - Value passed to the callback function
//DueTime - Time (milliseconds), before the timer is set to the signaled state for the first time
//Period - Timer period (milliseconds). If zero, timer is signaled only once
//Flags - One or more of the next values (table taken from MSDN):
//set the timer to call the timer routine in 2ms
if(!CreateTimerQueueTimer( &HTimer1, HTimerQueue1, (WAITORTIMERCALLBACK)TASK, &arg, 2,0,0))
{
printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());
return 3;
}
//Do other work here
printf("Call timer routine in 2 milliseconds...\n");
// wait for the timeröqueue thread to complete using an event
if (WaitForSingleObject(Task1, INFINITE) !=WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
CloseHandle(Task1);
//Delete all timers in the timer queue
if(!DeleteTimerQueue(HTimerQueue1))
printf("DeleteTimerQueue failed (%d)\n", GetLastError());
return 0;
}
我创建了一个名为 Task(task 2ms) 的函数,它每 2ms 调用一次。所以我为此创建了一个计时器队列。如果我喜欢这样,那么每 2 毫秒就会调用一次任务函数。这是正确的吗?