Chapter11-"windows线程池"之 间隔执行函数
按照一定的时间间隔,周期性调用某个函数,大致需要用到以下五步
#include <windows.h>#include <tchar.h>#include <stdio.h>#include <time.h>VOID CALLBACK TimeoutCallback( PTP_CALLBACK_INSTANCE pInstance, PVOID pvContext, PTP_TIMER pTimer ){ time_t now_time; now_time = time(NULL); struct tm *tblock; tblock = localtime(&now_time); printf("Local time is: %s",asctime(tblock)); }void main(){ // Create the threadpool timer object PTP_TIMER lpTimer = CreateThreadpoolTimer(TimeoutCallback, NULL, NULL); // Start the timer in one second to trigger every 1 second ULARGE_INTEGER ulRelativeStartTime; ulRelativeStartTime.QuadPart = (LONGLONG) -(10000000); // start in 1 second FILETIME ftRelativeStartTime; ftRelativeStartTime.dwHighDateTime = ulRelativeStartTime.HighPart; ftRelativeStartTime.dwLowDateTime = ulRelativeStartTime.LowPart; SetThreadpoolTimer( lpTimer, &ftRelativeStartTime, 1000, // Triggers every 1000 milliseconds 0 ); getchar(); WaitForThreadpoolTimerCallbacks(lpTimer, FALSE); // Clean up the timer CloseThreadpoolTimer(lpTimer); printf("ThreadpoolTimes is closed!\n"); getchar(); }《windows核心编程》(笔记)系列文章是本人看《windows核心编程》时的一些学习笔记,有疏忽之处,欢迎各位网友指正。QQ邮箱:job.zhanghui@qq.com