Date: 2016apr23
Update: 2026oct7
OS: Windows
Platform: win32
Keywords: InitTimer, StartTimer
Language: C/C++
Q. Windows: How to have timer without a thread
A. Use Windows' timers.
Initialize it:
enum { IDT_TIMER1 = 1000 }; // Any positive integer will do
SetTimer(hwnd, // handle to main window
IDT_TIMER1, // timer identifier
10000, // 10-second interval
NULL); // Send a windows message, don't call a function
Then you'll get a WM_TIMER message:
switch(message) {
case WM_TIMER:
switch (wParam) {
case IDT_TIMER1:
// Do something with the timer
return 0;
}
Stop it:
KillTimer(hwnd, IDT_TIMER1);