WaitableTimer 的特殊之处
2013-08-14 15:16
134 查看
#define _SECOND 10000000 //回调函数 VOID CALLBACK TimerAPCProc(LPVOID lpArg,DWORD dwTimerLowValue,DWORD dwTimerHighValue ) { printf("Timer was signaled.\n"); }
int _tmain(int argc, _TCHAR* argv[]) { LARGE_INTEGER liDueTime; //创建WaitableTimer HANDLE hTimer = CreateWaitableTimer(NULL, TRUE, NULL); if(hTimer) { //2秒后启用 liDueTime.QuadPart = -2 * _SECOND; //设置计时器属性 int result = SetWaitableTimer(hTimer,&liDueTime,1000,TimerAPCProc,NULL,FALSE); if(0< result) { printf("Waiting for 2 seconds...\n"); while(true) //如果此处不循环调用Ex函数,是不执行回调函数的 { /*SleepEx(INFINITE, TRUE);*/ WaitForSingleObjectEx(hTimer, INFINITE,TRUE); //调用SleepEx和WaitForSingleObjectEx效果等效 } } } return 0; }
查MSDN,才搞明白:
线程必须是由于调用 SleepEX,WaitForSingleObjectEx,WaitForMultipleObjectsEX,MSGWaitForMultipleObjectsEx 或 SignalObjectAndWait而进入等待的状态。否则,系统不会把计时器的APC函数添加到队列中。
This thread must be in an alertable state to execute the completion routine. It accomplishes this by calling the SleepEx function, which is an alertable function.
相关文章推荐
- 多线程编程(14) - 多线程同步之 WaitableTimer (等待定时器对象)
- 并发学习之:等待定时器(Waitable Timer)
- WaitableTimer
- Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数
- 搜集的一些关于CreateWaitableTimer的资料_等待定时器
- CreateWaitableTimer编译通不过
- Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数
- 多线程编程(14) - 多线程同步之 WaitableTimer (等待定时器对象)
- 多线程编程(15) - 多线程同步之 WaitableTimer (等待定时器对象)[续]
- 内核对象--可等待计时器WaitableTimer(一)
- Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数
- Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数
- CreateWaitableTimer和SetWaitableTimer函数
- 多线程编程(15) - 多线程同步之 WaitableTimer (等待定时器对象)[续]
- CreateWaitableTimer与SetWaitableTimer详解
- 多线程编程(16) - 多线程同步之 WaitableTimer (等待定时器对象)[续二]
- CreateWaitableTimer和SetWaitableTimer函数(定时器)
- 多线程同步之 WaitableTimer (等待定时器对象)
- 搜集的一些关于CreateWaitableTimer的资料_等待定时器
- 基于内核对象WaitableTimer实现的Timer组件