SleepEx();
// WaitableTimer.cpp : Defines the entry point for the console application. // #include "stdafx.h" #define _WIN32_WINNT 0x0400 #include "windows.h" HANDLE g_hTimer = NULL; DWORD WINAPI TimerThread( LPVOID pParam ) { while( 1 ) { WaitForSingleObject( g_hTimer, INFINITE ); printf( "Hello Timer\n" ); } return 0; } void Create( ) { //创建定时器 g_hTimer = CreateWaitableTimer( NULL, FALSE, NULL ); //设置定时器 UINT64 nDueTime = -100000000; SetWaitableTimer( g_hTimer, (PLARGE_INTEGER)&nDueTime, 1000, NULL, NULL, FALSE ); //创建等候线程 DWORD dwThreadID = 0; HANDLE hThread = CreateThread( NULL, 0, TimerThread, NULL, 0, &dwThreadID ); WaitForSingleObject( hThread, INFINITE ); //关闭定时器 CloseHandle( g_hTimer ); } VOID CALLBACK TimerProc( LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue, DWORD dwTimerHighValue ) { printf( "------APC TimerProc--------\n" ); } void APCTimer( ) { //创建定时器 HANDLE hTimer = CreateWaitableTimer( NULL, FALSE, NULL ); //设置定时器 UINT64 nDueTime = -10000000; SetWaitableTimer( hTimer, (PLARGE_INTEGER)&nDueTime, 1000, TimerProc, NULL, FALSE ); // while( 1 ) { SleepEx( -1, TRUE ); } //关闭句柄 CloseHandle( hTimer ); } int main(int argc, char* argv[]) { //Create( );//等候定时器 APCTimer( );//回调函数方式触发 return 0; }