日期:2014-05-17  浏览次数:20967 次

Chapter11-"windows线程池"之 间隔执行函数

     按照一定的时间间隔,周期性调用某个函数,大致需要用到以下五步

  1. 按照一定的时间间隔,周期性被调用的TimerCallback函数的原型如下
    VOID CALLBACK TimerCallback(
      _Inout_      PTP_CALLBACK_INSTANCE Instance,
      _Inout_opt_  PVOID Context,
      _Inout_      PTP_TIMER Timer
    );
  2. 调用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则在默认环境下执行
    );
    
  3. 调用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  // 定时器执行的时间窗口。
    );
    
  4. 调用 WaitForThreadpoolTimerCallbacks 函数等待 PTP_TIMER 的回调函数返回,并指定是否取消挂起的 PTP_TIMER 请求。
  5. 调用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