可等待计时器和用户计时器的最大区别在于用户计时器需要在应用程序中使用大量的用户界面基础设施,从而消耗更多的资源。此外,可等待计时器是内核对象,这意味着
他们不仅可以在多个线程间共享,而且具备安全性。
用户计时器会产生WM_TIMER消息,这个消息被送回条用SetTimer的线程(基于回调计时器)或者被送回创建窗口的线程(基于窗口的计时器)。因此,当一个用户计时器
触发的时候,只有一个线程会得到通知。
unsigned APIENTRY ThreadProc(void *p) { WaitForSingleObject(g_hWaitableTimer, INFINITE); MessageBox(NULL, _T("线程开始启动"), _T("HELLO"), MB_OK); return 0; } void APIENTRY ApcCompletionRoutine(PVOID pParm, DWORD dwTimerLowValue, DWORD dwTimeHighValue) { MessageBox(NULL, _T("ApcCompletionRoutine开始启动"), _T("HELLO"), MB_OK); return; } int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。 unsigned ThreadId; HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadProc, NULL, NULL, &ThreadId); g_hWaitableTimer = CreateWaitableTimer(NULL, FALSE, NULL); LARGE_INTEGER liStartTime; liStartTime.QuadPart = -(5 * 1000 * 1000 * 10); SetWaitableTimer(g_hWaitableTimer, &liStartTime, 0, ApcCompletionRoutine, NULL, FALSE); SleepEx(INFINITE, TRUE); // 关键 CloseHandle(hThread); CloseHandle(g_hWaitableTimer); MSG msg; HACCEL hAccelTable; // 初始化全局字符串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_WAITABLETIMERTEST, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WAITABLETIMERTEST)); // 主消息循环: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; }