c++ - 在带有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;
}
}
解决方案
推荐阅读
- amazon-web-services - SSL 问题:具有负载均衡器和外部域的 AWS Beanstalk
- reactjs - Material UI 在 value 属性中使用一个对象选择多个组件,添加重复条目
- azure-iot-hub - IoT 中心设备孪生中的 etag 有什么用?
- netsuite - 使用 record.load() 加载子记录
- html - 为什么我的页面没有显示任何引导布局?
- apache-kafka - 来自 Kafka 的 Flink SQL CLI 客户端 CREATE TABLE
- shader - 片段着色器的 Spark AR 脚本中的点积错误
- javascript - 使用 jQuery 添加 onClick 方法
- .net - 实体框架 ThenInclude 不包括孙子的属性
- java - Java 8 流对列表进行分组