线程中使用多媒体定时器

 

 

#include <mmsystem.h>
#pragma comment(lib, "Winmm.lib")

static DWORD TimePeriodicThread(LPVOID lParam)
{
  ULONGLONG uLL_TickCount=0;

  HANDLE hTimeEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

  MMRESULT timerID = timeSetEvent(1, //Event delay, in milliseconds
    0, //Resolution of the timer event
    (LPTIMECALLBACK)hTimeEvent, 
    NULL, TIME_PERIODIC|TIME_CALLBACK_EVENT_PULSE);
  ASSERT(timerID != NULL);

  if(timerID)
  {
    BOOL *bDo = (BOOL*)lParam;
    while(*bDo)
    {
      switch(WaitForSingleObject(hTimeEvent, INFINITE))
      {
      case(WAIT_OBJECT_0): //time event
        {
          uLL_TickCount++;
          break;
        }
      default:
        {
          ASSERT(FALSE);
          *bDo = FALSE;
          break;
        }
      }
    }

    VERIFY(timeKillEvent((UINT)timerID) == TIMERR_NOERROR );
  }

  CloseHandle(hTimeEvent);

  return (0);
}


 

//测试
{
  
  BOOL bDo = TRUE;
  HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TimePeriodicThread, &bDo, 0, NULL);

  Sleep(5000);
  bDo = FALSE;

  CloseHandle(hThread);
}


 

你可能感兴趣的:(线程中使用多媒体定时器)