gpt4 book ai didi

c++ - 使用 CreateTimerQueueTimer、Visual Studio 2012、C++ 创建计时器,定期运行

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:53:42 24 4
gpt4 key购买 nike

我正在尝试使用 CreateTimerQueueTimer(...) 每隔一段时间运行一个函数。

我正在使用来自 MSDN 的示例,主要是这一行与我有关:

CreateTimerQueueTimer( &hTimer, hTimerQueue,(WAITORTIMERCALLBACK)TimerRoutine, &arg , 50,100, 0)

语法是:

BOOL WINAPI CreateTimerQueueTimer(
_Out_ PHANDLE phNewTimer,
_In_opt_ HANDLE TimerQueue,
_In_ WAITORTIMERCALLBACK Callback,
_In_opt_ PVOID Parameter,
_In_ DWORD DueTime,
_In_ DWORD Period,
_In_ ULONG Flags
);

倒数第二个参数状态

Period [in]

The period of the timer, in milliseconds. If this parameter is zero, the timer is signaled once. If this parameter is greater than zero, the timer is periodic. A periodic timer automatically reactivates each time the period elapses, until the timer is canceled.

正如您在我的代码中看到的那样,我将到期时间设置为 50,将周期设置为 100。当我运行它时,它不会重复 触发计时器。有人可以帮我解决这个问题吗?

完整代码如下:

#include "stdafx.h"
#include <string>
#include <iostream>
#include <thread>
#include <Windows.h>
#include <stdio.h>

using namespace std;

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,x;


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

// Set a timer to call the timer routine in 10 seconds.
if (!CreateTimerQueueTimer( &hTimer, hTimerQueue,(WAITORTIMERCALLBACK)TimerRoutine, &arg , 50,100, 0))
{
printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());
return 3;
}

// TODO: Do other useful work here

printf("Call timer routine in 10 seconds...\n");

// Wait for the timer-queue thread to complete using an event
// object. The thread will signal the event at that time.

if (WaitForSingleObject(gDoneEvent, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());

CloseHandle(gDoneEvent);

// Delete all timers in the timer queue.
if (!DeleteTimerQueue(hTimerQueue))
printf("DeleteTimerQueue failed (%d)\n", GetLastError());
cin>>x;
return 0;
}

谢谢

最佳答案

事件 gDoneEvent 在函数 TimerRoutine() 退出之前发出信号。对于回调函数的重复调用,在函数 TimerRoutine() 被调用所需次数后,向事件 gDoneEvent 发出信号。

可以包含以下代码行。

static int count = 0;

VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
......
count++;
if(count == 100)
{
SetEvent(gDoneEvent);
}
}

关于c++ - 使用 CreateTimerQueueTimer、Visual Studio 2012、C++ 创建计时器,定期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23633437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com