Is it possible to have more than one timer in one window/application? Want to animate different objects on different times, or timings!
This is a discussion on Multiple timers on single window within the Windows Programming forums, part of the Platform Specific Boards category; Is it possible to have more than one timer in one window/application? Want to animate different objects on different times, ...
Is it possible to have more than one timer in one window/application? Want to animate different objects on different times, or timings!
I believe that's possible, as long as they are not the same ID. But you could of course also just do something like this:
That will perform the "doSomething" every 5th (count 0..4) tick - and it's most likely more lightweight than having two timers running simultaneously.Code:void handleTimer() { static int tCount = 0; tCount++; if (tCount > 3) { doSomething; tCount = 0; } doEveryTick(); }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I wrote a better one long ago.
If it's any use, for C, here it is:
Clock
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Although it doesn't use the use the timer mechanism.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Are these timers handled as threads, i mean which gets called first? Also, my idea is to simultaneously run them on different speeds (i.e. move two objects on screen on different speeds)Code:// Set two timers. SetTimer(hwnd, // handle to main window IDT_TIMER1, // timer identifier 10000, // 10-second interval (TIMERPROC) NULL); // no timer callback SetTimer(hwnd, // handle to main window IDT_TIMER2, // timer identifier 300000, // five-minute interval (TIMERPROC) NULL); // no timer callback /* To process the WM_TIMER messages generated by these timers, add a WM_TIMER case statement to the window procedure for the hwnd parameter. */ case WM_TIMER: switch (wParam) { case IDT_TIMER1: // process the 10-second timer return 0; case IDT_TIMER2: // process the five-minute timer return 0; }
You need not worry about which gets called first. Windows handles that.
Btw, the ID, not the handle, is sent along with the message.
And don't cast the address of the callback function. Ever.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
The way a timer works is that it posts a message to your message queue with the WM_TIMER message-type, and with the relevant ID. The messages for multiple timers may be posted at once by the OS. It is then up to your message-handler (aka event-loop) to handle the WM_TIMER message - this normally means that one happens after the other, but theoretically you could come up with (complicated) code that attempts to run multiple timer event's functions at once.
I hope this clarifies things - if not, please ask whatever it is you don't understand.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
If either of the timers has to perform a lenghty task (ie process large data files into a DB), remember to KillTimer(), perform the task and start the timer again.
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
I would also caution that as long as you are processing the WM_TIMER message, you are blocking other messages from being handled, as well.
To this, there are two solutions.
1) Process WM_TIMER in another thread.
2) Keep it short.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^