WaitableTimer的两种用法

原帖来自csdn论坛: http://topic.csdn.net/u/20090121/09/36a84fdc-167b-4f1d-b1a8-ce5e5be5bbf7.html

HANDLE CreateWaitableTimer(
  LPSECURITY_ATTRIBUTES lpTimerAttributes,  //安全描述符,可以为NULL

  BOOL bManualReset,                        //是否为手动定时器,如果是手动的,需要调用SetWaitableTimer才能将定时器变成信号的,

                                            //如果是自动的,则调用WaitForsingleObject即可实现定时器信号的重置
  LPCTSTR lpTimerName                       //定时器名称,这对于进程间的定时器来说是有用的。
);

SetWaitableTimer有两种用法,
第一种是设置定义器信号态时间,对于自动重置等待定时器,一时变成有信号的,那么WaitforsingleObject函数就会返回,并且,定时器变成非信号的,这时需要设置这个函数中的参数二,参数三,四,五被忽略。
第二种是设置定义器的周期响应时间,这种用法与普通定时器类似,不过要提供APC回调函数。这时需要设置这个函数中的参数三,四,五,参数二被忽略

第一方法需要将WaitForSingleObject放在一个循环中,每次调用SetWaitableTimer来重置信号:
HANDLE hTimer = NULL; 
LARGE_INTEGER liDueTime; 

liDueTime.QuadPart=-100000000; 

// Create a waitable timer. 
hTimer = CreateWaitableTimer(NULL, TRUE, _T("WaitableTimer")); //设置成手动重置定时器
if (!hTimer) 
{ 
    printf("CreateWaitableTimer failed (%d)\n", GetLastError()); 
    return 1; 
} 

printf("Waiting for 10 seconds...\n"); 

// Set a timer to wait for 10 seconds. 
if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0)) 
{ 
    printf("SetWaitableTimer failed (%d)\n", GetLastError()); 
    return 2; 
} 

// Wait for the timer. 
while(true) 
{ 
    if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0) 
    { 
        printf("WaitForSingleObject failed (%d)\n", GetLastError());
    } 
    else 
    { 
        printf("Timer was signaled.\n"); 
        SetWaitableTimer(hTimer,&liDueTime,0,NULL,NULL,0); 
    }
} 
这样一来,第10秒钟会显示出“Timer was signaled.”





第二个方法
#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <stdio.h>

#define _SECOND 10000000

typedef struct _MYDATA {
    TCHAR *szText;
    DWORD dwValue;
} MYDATA;

VOID CALLBACK TimerAPCProc(
        LPVOID lpArg, // Data value
        DWORD dwTimerLowValue, // Timer low value
        DWORD dwTimerHighValue ) // Timer high valu
{
    MYDATA *pMyData = (MYDATA *)lpArg
    printf( "Message: %s\nValue: %d\n\n", pMyData->szText,
    pMyData->dwValue );
    MessageBeep(0)
}

void main( void ) 
{
    HANDLE hTimer;
    BOOL bSuccess;
    __int64 qwDueTime;
    LARGE_INTEGER liDueTime;
    MYDATA MyData;
    TCHAR szError[255];

    MyData.szText = "This is my data.";
    MyData.dwValue = 100;

    if ( hTimer = CreateWaitableTimer( NULL, // Default security attributes
                                       FALSE, // Create auto-reset timer
                                       "MyTimer" ) ) // Name of waitable timer
    {
        __try 
        {
             bSuccess = SetWaitableTimer( hTimer, // Handle to the timer object
                                          0,
                                          2000, // Periodic timer interval of 2 seconds
                                          TimerAPCProc, // Completion routine
                                          &MyData, // Argument to the completion routine
                                          FALSE ); // Do not restore a suspended system
        
            if ( bSuccess ) 
            {
                for ( ; MyData.dwValue < 1000; MyData.dwValue += 100 )
                {
                    SleepEx( INFINITE, // Wait forever
                             TRUE ); // Put thread in an alertable state
                }
            } 
            else 
            {
                wsprintf( szError, "SetWaitableTimer failed with Error \
                                   %d.", GetLastError() );
                MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );
            }
        } 
        __finally 
        {
            CloseHandle( hTimer );
        }
    } 
    else 
    {
        wsprintf( szError, "CreateWaitableTimer failed with Error %d.", 
                  GetLastError() );
        MessageBox( NULL, szError, "Error", MB_ICONEXCLAMATION );
    }
}



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值