使用等待计时器。回调将在第 5 个参数设置的时间后调用,并每隔 6 个参数重复一次。时间过去后,回调被调用,执行一些处理并设置事件允许退出
// ConsoleTimer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#define TIME_LAPS 5 * 1000
HANDLE g_hExitEvent = NULL;
void CALLBACK WaitOrTimerCallback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
{
Sleep(5000); // fake long processing
SetEvent(g_hExitEvent);
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hNewTimer = NULL; //call after
BOOL IsCreated = CreateTimerQueueTimer(&hNewTimer, NULL, WaitOrTimerCallback, NULL, TIME_LAPS,
// repeat
TIME_LAPS, WT_EXECUTELONGFUNCTION);
g_hExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
WaitForSingleObject(g_hExitEvent, 15000);
DeleteTimerQueueTimer(NULL, hNewTimer, NULL);
return 0;
}
我不检查错误,你应该。