Can you clearly explain why the code below triggers the timer twice from time to time, say once in 5-10 executions? MSDN does not explain this weird phenomenon at all.
#if !defined(BTF)
#define BTF(b) (b ? _T('T') : _T('F'))
#endif
#if !defined(ERR)
#define ERR(b, d) (b ? 0 : d)
#endif
BOOL CWTApp::InitInstance()
{
CWinApp::InitInstance();
HANDLE hTimer = CreateWaitableTimer( NULL, FALSE, NULL );
BOOL bRet;
DWORD dwErr;
#define WAITABLETIMER_DUETIME_INITIALLY (0)
#define WAITABLETIMER_PERIOD (USER_TIMER_MINIMUM * 200)
#define SLEEP_BEFORE_CWT Sleep(50)
LARGE_INTEGER DueTime;
DueTime.QuadPart = WAITABLETIMER_DUETIME_INITIALLY;
bRet = SetWaitableTimer(hTimer, &DueTime, WAITABLETIMER_PERIOD, NULL, NULL, FALSE);
ATLTRACE(_T("SetWaitableTimer_1(hTimer)=%c/%d\n"), BTF(bRet), ERR(bRet, GetLastError()));
dwErr = WaitForSingleObject(hTimer, INFINITE);
ATLTRACE(_T("Timer(1) was signaled. dwErr=%d\n"), dwErr);
SLEEP_BEFORE_CWT;
bRet = CancelWaitableTimer(hTimer);
ATLTRACE(_T("CancelWaitableTimer(hTimer)=%c\n"), BTF(bRet), ERR(bRet, GetLastError()));
dwErr = WaitForSingleObject(hTimer, INFINITE);
ATLTRACE(_T("Timer(2) was signaled. dwErr=%d\n"), dwErr);
bRet = CloseHandle(hTimer);
return FALSE;
}