CreateWaitableTimer与SetWaitableTimer详解
来源:互联网 发布:win8无法连接到此网络 编辑:程序博客网 时间:2024/04/27 14:20
可等待定时器问题:
1. 需要的函数:CreateWaitableTimer,SetWaitableTimer.前者创建一个可等待定时器句柄,后者设置等待定时器触发的条件.我们可以多次调用SetWaitableTimer来重置可等待定时器.重置后之前的定时器将关闭.
2. 主要看两段代码:
代码一:(这段代码在网上出现的概率是较大的)
HANDLE hTimer = CreateWaitableTimer( NULL,FALSE,NULL );LARGE_INTEGER li;li.QuadPart = 0;if( !SetWaitableTimer( hTimer,&li,1000,NULL,NULL,FALSE )){CloseHandle( hTimer );return;}while ( 1 ){//clock_t c_beg = clock();WaitForSingleObject( hTimer,INFINITE );//clock_t end = clock() - c_beg;//cout<<"time:"<<end<<endl;cout<<"定时器触发了...\n";}CloseHandle(hTimer);
通过将上面注释的代码放开,我们可以发现:
◆ 前两个定时器出发过程”不稳定”(即它们所耗费的时间并不是1秒.
◆ 就算是稳定了以后,定时器的差距并不是1s而是1000(毫秒)左右,这就奇怪了.
如果说第二个问题是由于执行clock而产生的误差的话,那么第一个问题则没办法解释.(其实对于clock的时间几乎是可以忽略不计的.)
这样就引入了核心编程里面作者的代码:
代码二:
HANDLE hTimer = CreateWaitableTimer( NULL,FALSE,NULL );LARGE_INTEGER li;li.QuadPart = 0;if( !SetWaitableTimer( hTimer,&li,1000,NULL,NULL,FALSE )){CloseHandle( hTimer );return;}while ( 1 ){clock_t c_beg = clock();SleepEx( 1000,TRUE );//这里并没有使用WaitForSingleObject,而是使用SleepExclock_t end = clock() - c_beg;cout<<"time:"<<end<<endl;}CloseHandle(hTimer);
运行结果发现正常:每次都是1000(毫秒).
从这里可以间接证明,clock消耗的时间可以忽略不计.
再来看看SleepEx是怎么定义的.
DWORD WINAPI SleepEx(
DWORD dwMillesconds,
BOOL bAlertable);
这里主要说明第二个参数的用途:线程是否将自己置为可提醒状态.(在可提醒I/O中,当I/O完成时,系统将他们添加到线程APC队列中-回调函数不会立即被调用.如果我们想让其被调用需要将这个参数设置为TRUE).
本例并没有牵涉到这类问题,所以无论FALSE或者TRUE都无所谓.
如果我们只想要定义一个一次性定时器.可以参考如下代码:
HANDLE hTimer = NULL;LARGE_INTEGER liDueTime; liDueTime.QuadPart = -100000000;//10shTimer = CreateWaitableTimer(NULL, TRUE,NULL); if ( hTimer == NULL ) return; if (!SetWaitableTimer(hTimer, &liDueTime, 1000, NULL, NULL, 0)) { CloseHandle(hTimer); return;}clock_t c_beg = clock();if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0){CloseHandle(hTimer);return ;}else{//操作}CloseHandle(hTimer);
最后需要说明的一点是APC(异步调用过程)加入定时器中:
VOID APIENTRY TimerAPCRoutine( PVOID pvArgToCompletionRoutine,DWORD dwTimerLowValue,DWORD dwTimerHighValue){cout<<"high:"<<dwTimerHighValue<<" low"<<dwTimerLowValue<<endl;}void main(){HANDLE hTimer = CreateWaitableTimer( NULL,FALSE,NULL );LARGE_INTEGER li;li.QuadPart = 0;if( !SetWaitableTimer( hTimer,&li,1000,TimerAPCRoutine,NULL,FALSE )){CloseHandle( hTimer );return;}while ( 1 ){SleepEx( 1000,TRUE );}CloseHandle( hTimer );getchar();}
- CreateWaitableTimer与SetWaitableTimer详解
- CreateWaitableTimer与SetWaitableTimer详解
- CreateWaitableTimer和SetWaitableTimer函数
- CreateWaitableTimer和SetWaitableTimer函数
- CreateWaitableTimer和SetWaitableTimer函数
- CreateWaitableTimer和SetWaitableTimer函数
- Windows API CreateWaitableTimer和SetWaitableTimer
- CreateWaitableTimer和SetWaitableTimer函数(定时器)
- CreateWaitableTimer和SetWaitableTimer函数(定时器)
- CreateWaitableTimer和SetWaitableTimer函数(转载)
- CreateWaitableTimer和SetWaitableTimer函数(定时器)
- CreateWaitableTimer和SetWaitableTimer函数(定时器)
- CreateWaitableTimer和SetWaitableTimer函数(定时器)
- ns级定时器,CreateWaitableTimer和SetWaitableTimer
- Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数
- Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数
- Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数
- Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数
- codeForces #140 div 2
- win8 ListView基础
- 中断上下文可为和不可为事项
- Mac OS中SVN工具Versions日期重置脚本
- 浅谈插件机制
- CreateWaitableTimer与SetWaitableTimer详解
- 使用JS实现HTML Diff文本差异化比对功能
- C语言常见知识1(sizeof 和strlen,typedef和#define)
- 对Linux内核中进程上下文和中断上下文的理解
- Google提供的ADB工具包下载地址
- 笔试中最常用的字符串处理函数的实现
- java产生随机数的几种方式
- 关于单例
- 主窗体最小化时不显示在任务栏