负值表示相对时间,正值表示绝对时间,定时器精度为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. spring security在spring mvc的action中获取登录人信息

    @RequestMapping("/index") public ModelAndView login( @RequestParam(value = "error&quo ...

  2. VBOX不能为虚拟电脑打开一个新任务解决方法

    第二种方法亲测有效! http://jingyan.baidu.com/article/4f7d5712da0c131a2119277a.html

  3. codeforces763C

    解题报告: 直接截图好了...辣鸡博客园不能上传文件

  4. 初始化dataframe

    由字典生成dataframe: >>> d = {'col1': [1, 2], 'col2': [3, 4]} >>> df = pd.DataFrame(dat ...

  5. nova Scheduling 配置

    Nova中调度配置: scheduler_driver_task_period = scheduler_driver = nova.scheduler.filter_scheduler.FilterS ...

  6. MySql基础学习-mysql安装

    Linux环境下的安装 1检查是否已经安装 sudo service mysql start #若未安装,则提示: mysql: unrecognized service 2安装MySql #安装 M ...

  7. 机器学习(四)—逻辑回归LR

    逻辑回归常见问题:https://www.cnblogs.com/ModifyRong/p/7739955.html 推导在笔记上,现在摘取部分要点如下: (0) LR回归是在线性回归模型的基础上,使 ...

  8. web自动化:selenium原理和元素定位(一)

    一. Selenium2 WebDriver 当Selenium2.x提出了WebDriver的概念后,它提供了完全另外的一种方式与浏览器交互 那就是利用浏览器原生的API,封装成一套更加面向对象的S ...

  9. spring boot 基础篇 -- 集成接口测试Swagger

    一.在pom.xml加入Swagger jar包引入 <dependency> <groupId>io.springfox</groupId> <artifa ...

  10. hdu 1849 Rabbit and Grass(nim)

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...