CreateWaitableTimer和SetWaitableTimer
2024-08-27 20:06:37
负值表示相对时间,正值表示绝对时间,定时器精度为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
最新文章
- CYQ.Data V5 从入门到放弃ORM系列:教程 - MAction类使用
- 动画的使用&mdash;Drawable Animation
- 基于socket的客户端和服务端聊天简单使用 附Demo
- Yii2中request的使用
- java环境基础步骤 svn
- Solr资料
- BizTalk开发系列(三十七) 性能监视器在BizTalk性能测试中的使用
- 51nod 1027大数乘法
- 数据库操作(C#)
- C语言下动态库相互调用
- Hibernate入门(2)- 不用配置用注解
- UVa297 Quadtrees
- Android TabActivity与Activity之间的动画跳转(主要Tabhost中跳转出来的动画效果解决)
- Entity Framework with MySQL 学习笔记一(拦截)
- [Swust OJ 322]--东6宿舍灵异事件(中缀表达式转化为后缀表达式的简单运用)
- springCloud项目练习
- Linux下的 >;, >;>;, <;, ps, |, grep, /dev/null
- jQuery基础操作
- 使用 Gradle 构建 Java 项目
- MyBatis 的动态 SQL 使用说明