320x100

HOWTO: 비동기 프로시저 호출을 이용하여 Waitable 타이머 사용 하기

Posted on 2009. 5. 12. 13:04
Filed Under Programming/Network


타이머(알람) 만들때 좋은 녀석이란다.
기회되면 한번 써보잣!! When..?

[CODE] // SetWaitableTimer.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#define _WIN32_WINNT 0x0400
#include "stdafx.h"

#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 value.

   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 {

    // Create a negative 64-bit integer that will be used to
    // signal the timer 5 seconds from now.
    qwDueTime = -5 * _SECOND;

    // Copy the relative time into a LARGE_INTEGER.
    liDueTime.LowPart  = (DWORD) ( qwDueTime & 0xFFFFFFFF );
    liDueTime.HighPart = (LONG)  ( qwDueTime >> 32 );

    bSuccess = SetWaitableTimer(
     hTimer,                 // Handle to the timer object.
     &liDueTime,             // When timer will become signaled.
     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 );             // IMPORTANT!!! The thread must be in an
      // alertable state to process the APC.
     }

    } 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 );
 }

}[/CODE]

출처: http://support.microsoft.com/kb/601487/ko

반응형

About

by 쑤기c

반응형