负值表示相对时间,正值表示绝对时间,定时器精度为100ns (1ns=1/10亿 s),所以 -50000000 代表5秒,详见MSDN。

程序一为自动重置(先等待5秒,然后每1秒输出一次):

#include "stdafx.h"
#include<Windows.h>
#include<iostream>
#include<time.h> using namespace std;
int main(){
LARGE_INTEGER li;
li.QuadPart = -; HANDLE hTimer = CreateWaitableTimer( NULL,FALSE,NULL );
if( !SetWaitableTimer( hTimer,&li,,NULL,NULL, )) {
cout<<"error"<<endl;
CloseHandle( hTimer );
return ;
}
while ( ){
clock_t c_beg = clock();
WaitForSingleObject(hTimer,INFINITE);
clock_t end = clock() - c_beg;
cout<<"time:"<<end<<endl;
}
CloseHandle(hTimer);
system("pause");
return ;
}

程序二为手动重置(每秒输出),其实当CreateWaitableTimer第二个参数为TRUE时(即手动重置),SetWaitableTimer的第三个参数是不起作用的:

#include "stdafx.h"
#include<Windows.h>
#include<iostream>
#include<time.h> using namespace std;
int main(){
LARGE_INTEGER li;
li.QuadPart = -; HANDLE hTimer = CreateWaitableTimer( NULL,TRUE,NULL );
if( !SetWaitableTimer( hTimer,&li,,NULL,NULL, )) {
cout<<"error"<<endl;
CloseHandle( hTimer );
return ;
}
while ( ){
clock_t c_beg = clock();
WaitForSingleObject(hTimer,INFINITE);
SetWaitableTimer( hTimer,&li,,NULL,NULL, );
clock_t end = clock() - c_beg;
cout<<"time:"<<end<<endl;
}
CloseHandle(hTimer);
system("pause");
return ;
}

程序三:APC(异步调用过程)加入定时器

见MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/ms686898%28v=vs.85%29.aspx

CreateWaitableTimer和SetWaitableTimer的更多相关文章

  1. CreateWaitableTimer和SetWaitableTimer函数(定时器)

    用户感觉到软件的好用,就是可以定时地做一些工作,而不需要人参与进去.比如每天定时地升级病毒库,定时地下载电影,定时地更新游戏里的人物.要想 实现这些功能,就可以使用定时器的API函数CreateWai ...

  2. windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器(用轮询的办法保持高精度)

    windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器可以有 100 纳秒也就是 1/10 微秒, 1/10000 毫秒的精度. 呵呵. SetWa ...

  3. SetTimer and CreateWaitableTimer的例子(静态函数设置为回调函数,瑞士的网页,有点意思)

    Timers (SetTimer and CreateWaitableTimer) in Windows   SetTimer The following example creates a time ...

  4. WINDOWS 同步(Interlocked,InterlockedExchangeAdd,Slim读/写锁,WaitForSingleObject,CreateWaitableTimer等等)

    NOTE0 在以下两种基本情况下,线程之间需要相互通信: 需要让多个线程同时访问一个共享资源,同时不能破坏资源的完整性: 一个线程需要通知其它线程某项任务已经完成 1.原子访问:Interlocked ...

  5. windows核心编程 - 线程同步机制

    线程同步机制 常用的线程同步机制有很多种,主要分为用户模式和内核对象两类:其中 用户模式包括:原子操作.关键代码段 内核对象包括:时间内核对象(Event).等待定时器内核对象(WaitableTim ...

  6. windows核心编程---第八章 使用内核对象进行线程同步

    使用内核对象进行线程同步. 前面我们介绍了用户模式下线程同步的几种方式.在用户模式下进行线程同步的最大好处就是速度非常快.因此当需要使用线程同步时用户模式下的线程同步是首选. 但是用户模式下的线程同步 ...

  7. Microsoft Win32 to Microsoft .NET Framework API Map

    Microsoft Win32 to Microsoft .NET Framework API Map .NET Development (General) Technical Articles   ...

  8. 第9章 用内核对象进行线程同步(2)_可等待计时器(WaitableTimer)

    9.4 可等待的计时器内核对象——某个指定的时间或每隔一段时间触发一次 (1)创建可等待计时器:CreateWaitableTimer(使用时应把常量_WIN32_WINNT定义为0x0400) 参数 ...

  9. 英文不好也能快速"记忆" API

    英文不好不要紧,把API函数导入打字练习类软件,即是练习打字速度,提高编程效率:也能短时间记忆API. 坚持每天打一遍,约2小时,连续打两周,会对API有很好的记忆,此方法是结合英文学习方法!以下是W ...

随机推荐

  1. JSON解析字符串

    JSON解析字符串 JSON 解析字符串时,应按严格的标准,否则无法解析: str1 = '{"str":"string","number" ...

  2. UVA 10909 Lucky Number(树状数组+二分+YY)

    此题测试时预处理等了很久,结果470ms过了...... 题意:开始不怎么懂,结果发现是这个: 波兰裔美国数学家斯塔尼斯拉夫·乌拉姆(Stanislaw Ulam)在20世纪50年代中期开发出了另一种 ...

  3. PAT1027. Colors in Mars (20)

    #include <iostream> using namespace std; string tbl="0123456789ABC"; int main() { in ...

  4. 初探MyBatis之HelloWorld(一)

    官方地址:https://github.com/mybatis/mybatis-3 准备: 官方中文文档地址:http://www.mybatis.org/mybatis-3/zh/getting-s ...

  5. vlan 配置

    在RedHat上配置vlan: ~]$ modinfo 8021q在ethX接口上配置vlan逻辑子接口: DEVICE=ethX. BOOTPROTO=none ONBOOT=yes IPADDR= ...

  6. keystone cache

    http://docs.openstack.org/juno/config-reference/content/section_keystone.conf.html http://docs.opens ...

  7. 报表研究之工具篇-VBA

    最近一直在研究VBA,写报表的工作.将所得的知识,经验总结一下,与大家分享. 工具篇,VBA 1.EXCEL一个最好用的功能就是录制宏,当一个函数拿不准要怎么写,用什么函数表示的时候,录制就帮了大忙了 ...

  8. 【疯了C#】神奇的换肤(一)

    之前看网上有换肤控件IrisSkin2的介绍,感觉很神奇,今天亲自做下试验,C#越来越有趣了,是Labview无法比拟的. 前期准备工作如下:下载后IrisSkin2.dll及匹配的皮肤. 其中ssk ...

  9. 存储过程,游标,异常捕捉 try catch 实例代码

    1.存储过程代码.  (数据库是 AdventureWorks) 存储过程用来将错误信息插入到指定的表中. 在别的地方可以调用存储过程插入错误信息(下面部分代码 生成一个表,如果表已经存在就会插入错误 ...

  10. 解决:SyntaxError: Non-ASCII character in file

    今天尝试用monkeyrunner脚本在夜神模拟器上安装并截图QQ,但是遇到了一些问题: from com.android.monkeyrunner import MonkeyRunner, Monk ...