Chapter11-"windows线程池"之 间隔执行函数
2013-01-26 19:25
218 查看
按照一定的时间间隔,周期性调用某个函数,大致需要用到以下五步
按照一定的时间间隔,周期性被调用的TimerCallback函数的原型如下
调用CreateThreadpoolTimer函数将TimerCallback函数通知给线程池(thread pool)
调用SetThreadpoolTimer函数向线程池注册由CreateThreadpoolTimer函数创建的 PTP_TIMER。
调用 WaitForThreadpoolTimerCallbacks 函数等待 PTP_TIMER 的回调函数返回,并指定是否取消挂起的 PTP_TIMER 请求。
调用CloseThreadpoolTimer函数关闭注册的定时器,系统会自动释放相关资源。
按照一定的时间间隔,周期性被调用的TimerCallback函数的原型如下
VOID CALLBACK TimerCallback( _Inout_ PTP_CALLBACK_INSTANCE Instance, _Inout_opt_ PVOID Context, _Inout_ PTP_TIMER Timer );
调用CreateThreadpoolTimer函数将TimerCallback函数通知给线程池(thread pool)
PTP_TIMER WINAPI CreateThreadpoolTimer( _In_ PTP_TIMER_CALLBACK pfnti, // 定时回调函数地址,也就是上面的TimerCallback函数地址 _Inout_opt_ PVOID pv, // 传递给TimerCallback函数的参数 _In_opt_ PTP_CALLBACK_ENVIRON pcbe //TimerCallback函数的执行环境,如果为NULL则在默认环境下执行 );
调用SetThreadpoolTimer函数向线程池注册由CreateThreadpoolTimer函数创建的 PTP_TIMER。
VOID WINAPI SetThreadpoolTimer( _Inout_ PTP_TIMER pti, // CreateThreadpoolTimer函数创建的PTP_TIMER _In_opt_ PFILETIME pftDueTime, // 定时器启动的时间(以100ns为单位) // 如果是负数,则是一个相对值,表示现在距离定时器函数首次执行的时间间隔 // 如果是正数,则表示首次执行时间距离 1600年1月1日 的时间间隔 _In_ DWORD msPeriod, // 指定定时器函数多久执行一次。如果msPeriod = 0,表示定时器函数之执行一次 _In_opt_ DWORD msWindowLength // 定时器执行的时间窗口。 );
调用 WaitForThreadpoolTimerCallbacks 函数等待 PTP_TIMER 的回调函数返回,并指定是否取消挂起的 PTP_TIMER 请求。
调用CloseThreadpoolTimer函数关闭注册的定时器,系统会自动释放相关资源。
#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
相关文章推荐
- Chapter11-"windows线程池"之 间隔执行函数
- Chapter11-"windows线程池"之异步调用函数
- Chapter11-"windows线程池"之异步调用函数
- Chapter11-"windows线程池" 之 内核对象触发调用回调函数
- Chapter11-"windows线程池" 之 内核对象触发调用回调函数
- JavaScript 函数队列按时间间隔顺序执行
- mkdir是windows的一个命令,也就是在命令行可以直接运行的命令。如果你在命令行中可以直接执行"python" 那么就可以替换执行
- js eval()函数 接收一个字符串,做为js代码来执行。 如: s='var d="kaka"'; 或者s=‘function (code){return code }’;
- "windows线程池" 之 内核对象触发调用回调函数
- "执行存储过程"“为过程或函数指定了过多的参数”的错误 .
- 关于JSP中<body onload="fun()">body标签中onload中函数不执行问题
- PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
- SQLserver不是可以识别的内置函数名称"的错误,通过set statistics time on能得到SQL语句的执行时间精确到毫秒.
- 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法
- PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
- Windows下利用C语言获得函数的执行时间
- Windows系统下获取一段函数执行的时间
- windows下php的exec函数执行不成功的另一种原因
- "crontab" 间隔固定时间执行