Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                                                                                            
                
        
#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(任务2ms)的函数,该函数每2ms调用一次。因此,我为此创建了一个计时器队列。如果我喜欢这样做,那么Task函数将每2ms调用一次。这是正确的吗?

最佳答案

...每2ms被调用一次。这是正确的吗?

不,那是不对的。

设置计时器队列计时器时,需要遵循以下文档:

您指定的DueTime为2毫秒!

DueTime:相对于当前时间(以毫秒为单位),必须在第一时间向定时器发出信号之前经过该时间。

并且您将Period指定为零!

计时器的时间(以毫秒为单位)。如果此参数为零,则向计时器发送一次信号。如果此参数大于零,则计时器为定期计时器。周期计时器每次在经过一段时间后都会自动重新激活,直到取消计时器。

您还必须将Period指定为2 ms。

但是您的代码仍然无法处理多个计时器事件。它仅在发生第一个计时器事件后结束。因此,您可能还是要花一些时间在代码上,例如:

while (1) {
  if (WaitForSingleObject(Task1, INFINITE) == WAIT_OBJECT_0) {
    printf("2 ms event occurred!\n");
  } else {
    printf("WaitForSingleObject failed (%d)\n", GetLastError());
    break;
  }
}


附注:Task2ms的作用是什么?并且:printf("Hello"):必须替换为printf("Hello\n");(使用分号作为终止符/语句分隔符!)。
您实际上是在询问从未尝试编写的代码。您不应该期望人们会热衷于回答此类问题。

07-24 21:36