zoukankan      html  css  js  c++  java
  • [转载]CreateWaitableTimer和SetWaitableTimer函数

    使用定时器的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  }

  • 相关阅读:
    韩式英语
    Daily dictation 听课笔记
    words with same pronunciation
    you will need to restart eclipse for the changes to take effect. would you like to restart now?
    glottal stop(britain fountain mountain)
    education 的发音
    第一次用Matlab 的lamada语句
    SVN的switch命令
    String的split
    SVN模型仓库中的资源从一个地方移动到另一个地方的办法(很久才解决)
  • 原文地址:https://www.cnblogs.com/Silence/p/2500892.html
Copyright © 2011-2022 走看看