可等待定时器
1. 代码中的for循环的 那个 sleepex表示什么意思?
其参数第一个是-1, 第二个是 true
msdn:
第一个参数:-1表示 挂起的不会超时
第二个参数为false.表示函数不会返回,只有在时间爱你周期消失后才会返回。
如果apc函数投递到线程的apc队列中,函数则不会返回,并且apc 函数也不会执行的。
如果是true,则对于apc函数,时间周期一旦消失,就会被调用。
这是我翻译的, 没怎么看懂,谁能够结合着代码,解释一下这2个参数!!!
2.可等待定时器什么情况下使用?平时见的很少。
3.可等待定时器涉及到一个概念:apc函数。 窗口回调函数,是不是属于apc?
线程函数属于嘛?
#define _WIN32_WINNT 0x0500#include < windows.h >#include < stdio.h > #define _SECOND 10000000typedef 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 an 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 ); // 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 ); }}