Dave's Brain

Browse - Programming Tips - How can I have timer without a thread?

Date: 2016apr23
OS: Windows
Platform: win32
Keywords: InitTimer, StartTimer

Q.  How can I have timer without a thread?

A.  Use Windows' timers.

Initialize it:
#define IDT_TIMER1 (1000) // Any number 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);

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.