Windows API一日一练(66)CreateWaitableTimer和SetWai...

来源:百度文库 编辑:神马文学网 时间:2024/04/28 22:44:37
  Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数 收藏
 Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数
用户感觉到软件的好用,就是可以定时地做一些工作,而不需要人参与进去。比如每天定时地升级病毒库,定时地下载电影,定时地更新游戏里的人物。要想实现这些功能,就可以使用定时器的API函数CreateWaitableTimer和SetWaitableTimer来实现了,这对API函数创建的时钟是比较精确的,可以达到100倍的10亿分之一秒。函数CreateWaitableTimer和SetWaitableTimer声明如下: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  }
发表于 @ 2009年01月09日 15:43:00 | 评论( 0 ) | 编辑| 举报| 收藏 旧一篇:Windows API一日一练(64) RegSetValueEx和RegDeleteValue函数 | 新一篇:Windows API一日一练(64) RegSetValueEx和RegDeleteValue函数
查看最新精华文章 请访问博客首页相关文章
Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数Windows API一日一练(61)GetDriveType函数Windows API一日一练(66)CreateWaitableTimer和SetWaitableTimer函数CreateWaitableTimer和SetWaitableTimer函数Windows API CreateWaitableTimer和SetWaitableTimer本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/li_guotao/archive/2009/01/09/3741326.aspx