负值表示相对时间,正值表示绝对时间,定时器精度为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. window7 共享wifi(不通过wifi软件)

    1.新建共享网络账号 管理员登录cmd输入:netsh wlan set hostednetwork mode=allow ssid=4Gtest key=12345678 ssid是无线网络名称.k ...

  2. mongod无法启动

    今天遇到了一个奇葩问题,我在Linux系统里备份了数据库,结果不知道为什么,系统用不了了,后来经过同事的检查,发现原来是我的硬盘快满了,导致mongod数据无法启动,真是.......

  3. source insigt、pc-lint、VS联合使用

    前言: 近几天参加公司培训,公司要求,开发的时候使用source insight.PC-lint和VC来编程和调试,这不用不知道,一用吓一跳,这套工具一组合简直爽的根本停不下来. 先说一下各自的作用, ...

  4. PAT1023. Have Fun with Numbers (20)

    #include <iostream> #include <map> #include <algorithm> using namespace std; strin ...

  5. webview 最简单的demo

    ) { return; } view.loadUrl(url); }} <!--activity_test.xml> <?xml version="1.0" en ...

  6. JavaWeb -- 邮件收发

    smtp协议: telnet smtp.qq.com 25 ehlo kevin auth login eGlhbmdqaWU1NUBxcS5jb20= a2V2aW5feGlhbmc1NQ== ma ...

  7. this关键字详解

    在java中,编译器会为每个对象分配一个this关键字.在代码中使用关键字可以使代码更优雅.下面我就列举一下this关键字常见的几种场景. 1.this代表当前对象调用成员变量和方法,也是用的最多的地 ...

  8. MFC 去掉CWnd的边框

    使用继承CWnd的控件,总是有边框,使用 ModifyStyle(WS_BORDER,0);  不能去掉边框,包括SetWindowLong设置去掉WS_BORDER, 也不行. 最后找到了方法就是M ...

  9. 185. Department Top Three Salaries

    问题描述 解决方案 select b.name Department,a.name Employee,a.salary Salary from Employee a,Department b wher ...

  10. DataGrid的组成论述(WPF)

    DataGrid:它标示是一个整体概念,是个大容器,包含Row的感念 Column:是表格的组成成分(表格是由列组成的),它包括Header和Cell的概念