windows_42_Thread_WaitableTimer 线程 - 等候线程
// windows_42_Thread_WaitableTimer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "windows.h"
HANDLE g_hTimer = NULL;
//等候线程
DWORD WINAPI TimerThread( LPVOID pParam )
{
while (true)
{
WaitForSingleObject( g_hTimer, INFINITE );
printf( "Hello Timer\n" );
}
}
VOID CALLBACK TimerAPCProc(
LPVOID lpArgToCompletionRoutine, // data value
DWORD dwTimerLowValue, // timer low value
DWORD dwTimerHighValue // timer high value
)
{
printf( "-------------APC TimerProc-------------\n" );
}
void APCTimer( )
{
//创建定时器
HANDLE hTimer = CreateWaitableTimer( NULL, FALSE, NULL );
//设置定时器
UINT64 nDueTime = -10000000;
SetWaitableTimer( hTimer, (PLARGE_INTEGER)&nDueTime, 1000, TimerAPCProc, NULL, FALSE );
while (true)
{
SleepEx( -1, TRUE );
}
//关闭句柄
CloseHandle( hTimer );
}
void Create( )
{
//创建定时器
g_hTimer = CreateWaitableTimer( NULL, FALSE, NULL );
//设置定时器
UINT64 nDueTime = -50000000;//百纳秒级别
SetWaitableTimer( g_hTimer, (PLARGE_INTEGER)&nDueTime/*转换成UINT64处理*/,
1000, NULL, NULL, FALSE );
//创建等候线程
DWORD dwThreadID = 0;
HANDLE hThread = CreateThread( NULL, 0, TimerThread, NULL,
0, &dwThreadID );
WaitForSingleObject( hThread, INFINITE );
//关闭定时器
CloseHandle( g_hTimer );
}
int _tmain(int argc, _TCHAR* argv[])
{
//Create( );
APCTimer( );
return 0;
}
时间: 2024-10-08 10:14:23