惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
V
V2EX
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
I
InfoQ
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
H
Help Net Security
腾讯CDC
D
Docker
P
Proofpoint News Feed
GbyAI
GbyAI
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏

博客园 - Silence

[转载]使用CPU时间戳进行高精度计时 [转载]VC实现CPU速度自测 自动刷新查询火车票脚本 北欧国旗的十字架 lu面 【转载】粤语翻译工具 异形魔方SQ1的暴力解法 [转载]聪明地使用Google的7个技巧 转载:“凤求凰”的解释,有才 【转】眼镜业内幕 专业操盘手的买卖法则 性格的英文单词 股东权益和权益比 音量控制面板项目说明 乘车指南 券商创设认购权证分析 调整出口关税对股价的影响 深市证券交易费用 沪市证券交易费用
[转载]CreateWaitableTimer和SetWaitableTimer函数
Silence · 2012-05-15 · via 博客园 - Silence

使用定时器的API函数CreateWaitableTimerSetWaitableTimer来实现了,这对API函数创建的时钟是比较精确的,可以达到100倍的10亿分之一秒,即100纳秒。
 
函数CreateWaitableTimerSetWaitableTimer声明如下:
 
WINBASEAPI
__out
HANDLE
WINAPI
CreateWaitableTimerA(
    __in_opt LPSECURITY_ATTRIBUTES lpTimerAttributes,
    __in     BOOL bManualReset,
    __in_opt LPCSTR lpTimerName
    );
WINBASEAPI
__out
HANDLE
WINAPI
CreateWaitableTimerW(
    __in_opt LPSECURITY_ATTRIBUTES lpTimerAttributes,
    __in     BOOL bManualReset,
    __in_opt LPCWSTR lpTimerName
    );
#ifdef UNICODE
#define CreateWaitableTimer CreateWaitableTimerW
#else
#define CreateWaitableTimer CreateWaitableTimerA
#endif // !UNICODE
 
 
WINBASEAPI
BOOL
WINAPI
SetWaitableTimer(
    __in     HANDLE hTimer,
    __in     const LARGE_INTEGER *lpDueTime,
    __in     LONG lPeriod,
    __in_opt PTIMERAPCROUTINE pfnCompletionRoutine,
    __in_opt LPVOID lpArgToCompletionRoutine,
    __in     BOOL fResume
    );
 
lpTimerAttributes是设置定时器的属性。
bManualReset是是否手动复位。
lpTimerName是定时器的名称。
hTimer是定时器的句柄。
lpDueTime是设置定时器时间间隔,当设置为正值是绝对时间;当设置为负数是相对时间。
lPeriod是周期。
pfnCompletionRoutine是设置回调函数。
lpArgToCompletionRoutine是传送给回调函数的参数。
fResume是设置系统是否自动恢复。
 
调用函数的例子如下:
#001 //创建定时器
#002  //蔡军生 2007/11/06 QQ:9073204 深圳
#003  int CreateTestTimer(void)
#004  {
#005         HANDLE hTimer = NULL;
#006         LARGE_INTEGER liDueTime;
#007 
#008         //设置相对时间为10秒。
#009         liDueTime.QuadPart = -100000000;
#010 
#011      ;   //创建定时器。
#012        hTimer = CreateWaitableTimer(NULL, TRUE, _T("TestWaitableTimer"));
#013         if (!hTimer)
#014         {              
#015               return 1;
#016         }
#017 
#018         OutputDebugString(_T("10秒定时器/r/n"));
#019 
#020         // 设置10秒钟。
#021        if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0))
#022         {        
#023               //
#024               CloseHandle(hTimer);
#025               return 2;
#026         }
#027 
#028         //等定时器有信号。
#029         if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
#030         {
#031               OutputDebugString(_T("10秒定时器出错了/r/n"));   
#032               //
#033               CloseHandle(hTimer);
#034               return 3;
#035         }
#036         else
#037         {
#038               //10秒钟到达。
#039               OutputDebugString(_T("10秒定时器到了/r/n"));            
#040         }
#041 
#042         //
#043         CloseHandle(hTimer);
#044         return 0;
#045  }