关于定时器

1. CreateTimerQueueTimer


 

// 创建并启动定时器
BOOL WINAPI CreateTimerQueueTimer(
 __out         PHANDLE phNewTimer,
 __in          HANDLE TimerQueue,
 __in          WAITORTIMERCALLBACK Callback,
 __in          PVOID Parameter,
 __in          DWORD DueTime,
 __in          DWORD Period,
 __in          ULONG Flags
);
// 删除定时器
BOOL WINAPI DeleteTimerQueueTimer(
 __in          HANDLE TimerQueue,
 __in          HANDLE Timer,
 __in          HANDLE CompletionEvent
);

  • 示例

        

点击(此处)折叠或打开

  1. // 创建定时器
  2. if (!CreateTimerQueueTimer(
  3.                     &timerId, // timer handle
  4. NULL, // the default timer queue
  5.                     (WAITORTIMERCALLBACK)handler, // 回调函数
  6.                     "test", // 传递给回调函数的数据
  7.                     0, // timer启动延迟时间(毫秒)
  8. 1000, // 周期(毫秒)
  9. 0))    // flags
  10.     {
  11.         printf("CreateTimerQueueTimer error.(%d)", GetLastError());
  12.     }
  13. // 回调函数
  14. static void CALLBACK handler(PVOID lpParam, BOOLEAN TimerOrWaitFired)
  15. {
  16. const char* str = (const char*)lpParam;
  17. printf("str=%s\n", str);
  18. }
  19. // 删除定时器
  20. DeleteTimerQueueTimer(
  21. NULL, // NULL, the default timer queue
  22. timerId, // timer handle
  23. INVALID_HANDLE_VALUE);    // 如果此参数设为NULL,有可能导致程序崩溃




 

2. timeSetEvent

// 创建并启动定时器
MMRESULT timeSetEvent(  
UINT           uDelay,      
UINT           uResolution,   
LPTIMECALLBACK lpTimeProc,  
DWORD_PTR      dwUser,        
UINT           fuEvent);
// 删除定时器
MMRESULT timeKillEvent(UINT uTimerID);

  • 示例

点击(此处)折叠或打开

  1. // 创建定时器
  2. timerId = timeSetEvent(1000, // 周期(毫秒)
  3. 100, // 精度(毫秒)
  4. &handler, // 回调函数
  5. (DWORD_PTR)"test",     // 传递给回调函数的数据
  6. // 定时器种别(一次性或周期性)
  7. TIME_PERIODIC | TIME_KILL_SYNCHRONOUS | TIME_CALLBACK_FUNCTION);
  8.     if (timerId == NULL)
  9.     {
  10.         printf("timeSetEvent error.(%d)", GetLastError());
  11.         return;
  12.     }
  13. // 回调函数
  14. static void CALLBACK handler(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
  15. {
  16. const char* str = (const char*)dwUser;
  17. printf("str=%s\n", str);
  18. }
  19. // 删除定时器
  20. timeKillEvent(timerId);

使用此种定时器函数需要依赖库文件Winmm.lib。

3. CreateWaitableTimer

// 创建定时器
HANDLE WINAPI CreateWaitableTimer(
 __in          LPSECURITY_ATTRIBUTES lpTimerAttributes,
 __in          BOOL bManualReset,
 __in          LPCTSTR lpTimerName
);
// 启动定时器
BOOL WINAPI SetWaitableTimer(
 __in          HANDLE hTimer,
 __in          const LARGE_INTEGER* pDueTime,
 __in          LONG lPeriod,
 __in          PTIMERAPCROUTINE pfnCompletionRoutine,
 __in          LPVOID lpArgToCompletionRoutine,
 __in          BOOL fResume
);
// 取消定时器,调用SetWaitableTimer可重新激活定时器
BOOL WINAPI CancelWaitableTimer(
 __in          HANDLE hTimer
);

  • 示例

点击(此处)折叠或打开

  1. // 创建定时器
  2. hTimer = CreateWaitableTimer(
  3.            NULL, // Default security attributes
  4.            FALSE, // Create auto-reset timer
  5.            NULL); // Name of waitable timer
  6. if (hTimer == NULL)
  7. {
  8.         printf("CreateWaitableTimer error.(%d)", GetLastError());
  9.         return;
  10. }
  11.    
  12. liDueTime.LowPart = 0;
  13. liDueTime.HighPart = 0;
  14. // 启动定时器
  15. bSuccess = SetWaitableTimer(
  16.                     hTimer, // Handle to the timer object
  17.                     &liDueTime, // When timer will become signaled
  18.                     1000, // Periodic timer interval
  19.                     handler, // Completion routine
  20.                     "test", // Argument to the completion routine
  21.                     FALSE); // Do not restore a suspended system
  22.     if (!bSuccess)
  23.     {
  24.         printf("SetWaitableTimer error.(%d)", GetLastError());
  25.         return;
  26.     }
  27. // 回调函数
  28. static void CALLBACK handler(
  29.   LPVOID lpArg, // Data value
  30.   DWORD dwTimerLowValue, // Timer low value
  31.   DWORD dwTimerHighValue ) // Timer high value */
  32. {
  33. const char* str = (const char*)lpArg;
  34. printf("str=%s\n", str);
  35. }
  36. // 删除定时器
  37. CancelWaitableTimer(timerId); // 取消激活
  38. CloseHandle(timerId);    // 关闭句柄

在使用此种定时器时,导致阻塞中的函数select返回值不正常,debug版本下返回-1,这个与linux下
中断发生相似,比较好处理,只要重新调用select函数就好了。但是不能理解的是在release版本下,
它返回正常且select的返回值大于1,也就是多个可读写的描述字准备好了,实际并没有,导致
在下面的处理中调用accept函数时出现异常,不知道为什么,只好放弃使用此种定时器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值