CreateWaitableTimer和SetWaitableTimer
负值表示相对时间,正值表示绝对时间,定时器精度为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的更多相关文章
- CreateWaitableTimer和SetWaitableTimer函数(定时器)
用户感觉到软件的好用,就是可以定时地做一些工作,而不需要人参与进去.比如每天定时地升级病毒库,定时地下载电影,定时地更新游戏里的人物.要想 实现这些功能,就可以使用定时器的API函数CreateWai ...
- windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器(用轮询的办法保持高精度)
windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器可以有 100 纳秒也就是 1/10 微秒, 1/10000 毫秒的精度. 呵呵. SetWa ...
- SetTimer and CreateWaitableTimer的例子(静态函数设置为回调函数,瑞士的网页,有点意思)
Timers (SetTimer and CreateWaitableTimer) in Windows SetTimer The following example creates a time ...
- WINDOWS 同步(Interlocked,InterlockedExchangeAdd,Slim读/写锁,WaitForSingleObject,CreateWaitableTimer等等)
NOTE0 在以下两种基本情况下,线程之间需要相互通信: 需要让多个线程同时访问一个共享资源,同时不能破坏资源的完整性: 一个线程需要通知其它线程某项任务已经完成 1.原子访问:Interlocked ...
- windows核心编程 - 线程同步机制
线程同步机制 常用的线程同步机制有很多种,主要分为用户模式和内核对象两类:其中 用户模式包括:原子操作.关键代码段 内核对象包括:时间内核对象(Event).等待定时器内核对象(WaitableTim ...
- windows核心编程---第八章 使用内核对象进行线程同步
使用内核对象进行线程同步. 前面我们介绍了用户模式下线程同步的几种方式.在用户模式下进行线程同步的最大好处就是速度非常快.因此当需要使用线程同步时用户模式下的线程同步是首选. 但是用户模式下的线程同步 ...
- Microsoft Win32 to Microsoft .NET Framework API Map
Microsoft Win32 to Microsoft .NET Framework API Map .NET Development (General) Technical Articles ...
- 第9章 用内核对象进行线程同步(2)_可等待计时器(WaitableTimer)
9.4 可等待的计时器内核对象——某个指定的时间或每隔一段时间触发一次 (1)创建可等待计时器:CreateWaitableTimer(使用时应把常量_WIN32_WINNT定义为0x0400) 参数 ...
- 英文不好也能快速"记忆" API
英文不好不要紧,把API函数导入打字练习类软件,即是练习打字速度,提高编程效率:也能短时间记忆API. 坚持每天打一遍,约2小时,连续打两周,会对API有很好的记忆,此方法是结合英文学习方法!以下是W ...
随机推荐
- mysql基础(1)-基本操作
数据库 数据库(Database,DB)是数据的集合,是一个长期存储在计算机内的.有组织的.有共享的.统一管理的数据集合. 存储数据 管理数据 数据库类型 关系型数据库:由二维表及其之间的联系组成的一 ...
- windows系统JDK的安装及环境配置
本文转载至:http://blog.csdn.net/sweetburden2011/article/details/8881181 一:JDK的安装 1. 首先上甲骨文公司的官方网站下载JDK的 ...
- flume-ng源码阅读RollingFileSink(原创)
org.apache.flume.sink.RollingFileSink 这个类比较简单. source的种类有两种:一种是PollableSource:另外一种是EventDrivenSou ...
- codeforces459D:Pashmak and Parmida's problem
Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心+优先队列
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- JMeter-Window10系统下设置环境变量
首先我们右击此电脑(我的电脑),点击属性 接下来我们就可以进入到控制面板主页,点击[高级系统设置] 在系统属性里面,点击[环境变量]按钮 在环境变量里面,点击[新建]按钮 接下来我们输 ...
- Spark- 共享变量
Shared Variables Normally, when a function passed to a Spark operation (such as map or reduce) is ex ...
- js字符串常用操作
1.字符串分割 var myStr = "I,Love,You,Do,you,love,me"; var substrArray = myStr .split(",&qu ...
- HTML代码中的空格和空行
HTML 代码中的所有连续的空格或空行(换行)都会被显示为一个空格. 例子1:(文本内容中的连续空格) 代码 <p>这段文本中,输入连续的空格 大概输入了十个.</p> 显示效 ...
- 剑指offer--8.调整数组顺序使奇数位于偶数前面
习惯了简单 ------------------------------------------------- 时间限制:1秒 空间限制:32768K 热度指数:422906 本题知识点: 数组 题目 ...