I took a working demo source of the waiting timer from microsoft, but it is worth setting the time in it for more than three minutes as it stops working correctly. That is, the timer is triggered instantly and without delay. What could be the reason?

#include <windows.h> #include <stdio.h> int main() { HANDLE hTimer = NULL; LARGE_INTEGER liDueTime; //liDueTime.QuadPart = -100000000LL; // оригинал liDueTime.QuadPart = -(10000000 * 300); // устанавливаем на 5 минут // Create an unnamed waitable timer. hTimer = CreateWaitableTimer(NULL, TRUE, NULL); if (NULL == hTimer) { printf("CreateWaitableTimer failed (%d)\n", GetLastError()); return 1; } printf("Waiting for 10 seconds...\n"); // Set a timer to wait for 10 seconds. if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0)) { printf("SetWaitableTimer failed (%d)\n", GetLastError()); return 2; } // Wait for the timer. if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0) printf("WaitForSingleObject failed (%d)\n", GetLastError()); else printf("Timer was signaled.\n"); return 0; } 

    1 answer 1

    What do you think is equal to 10000000 * 300 ? Do you think 3000000000 ? But no, it doesn’t fit into an int , so it will be trimmed ...

    In short, indicate explicitly that this is a long long :

     liDueTime.QuadPart = -(10000000ll * 300ll); 

    By the way, the compiler should have issued a warning! Have you seen him?

    • Well, to be honest, I guessed it, just relied on the compiler from microsoft, he thought he knew his system. And I already took to read about the format FILETIME. straight helped. thank you very much for the help. no compiler said anything to me. - perfect
    • Turn on the compiler warning level higher, and if you are a beginner - also "treat warnings as errors." If you use Visual C ++, I recommend the /W4 /WX keys - Harry
    • I'm aware of varningov. I didn’t use the studio for two years. I just had to write a program with winapi. I'm used to opensource. - perfect
    • Looked - Open Watcom, for example, when -w=3 also reports a problem. There are no others at hand :) - Harry
    • I usually don't trust the compiler when converting. but then both ide and os from microsoft and I thought well, wouldn't this bundle understand that you need a type more times on the left side of an INT64 expression. Stupid as it turns out. From the big to do a little that would then again make a big ... Why do we need such a converter. - perfect