首页 > 解决方案 > 在带有mingw编译器的Windows上使用微秒计时器?

问题描述

我想设置一个计时器。当时间到期时,它应该调用一个回调函数。但时间以微秒为单位。我在 Windows 上找到多媒体计时器,但我在 Windows 上使用 mingw 编译器。那么有没有在windows上使用mingw编译器的方法。

我曾尝试使用https://docs.microsoft.com/en-us/windows/win32/sync/using-timer-queues

但我有一个错误:'CreateTimerQueue() 没有在这个范围内声明'我该如何解决这个问题?

#include <windows.h>
#include <stdio.h>

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;

    // 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;
    }
}

标签: c++windowstimermingw

解决方案


推荐阅读