Use waitable timer. Callback will be called after time set by 5-th parameter and repeat every 6-th parameter.
After time elapses, callback is called, executes some processing and sets event to allow exit
// 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;
}
我不检查错误,你应该检查。