Windows API一日一練(66)CreateWaitableTimer和SetWaitableTimer函數

來源:互聯網
上載者:User
使用者感覺到軟體的好用,就是可以定時地做一些工作,而不需要人蔘與進去。比如每天定時地升級病毒庫,定時地下載電影,定時地更新遊戲裡的人物。要想實現這些功能,就可以使用定時器的API函數CreateWaitableTimer和SetWaitableTimer來實現了,這對API函數建立的時鐘是比較精確的,可以達到100倍的10億分之一秒。 函數CreateWaitableTimer和SetWaitableTimer聲明如下: WINBASEAPI__outHANDLEWINAPICreateWaitableTimerA(    __in_opt LPSECURITY_ATTRIBUTES lpTimerAttributes,    __in     BOOL bManualReset,    __in_opt LPCSTR lpTimerName    );WINBASEAPI__outHANDLEWINAPICreateWaitableTimerW(    __in_opt LPSECURITY_ATTRIBUTES lpTimerAttributes,    __in     BOOL bManualReset,    __in_opt LPCWSTR lpTimerName    );#ifdef UNICODE#define CreateWaitableTimer CreateWaitableTimerW#else#define CreateWaitableTimer CreateWaitableTimerA#endif // !UNICODE  WINBASEAPIBOOLWINAPISetWaitableTimer(    __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  }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.