Is it possible to have more than one timer in one window/application? Want to animate different objects on different times, or timings!
Printable View
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
I wrote a better one long ago.
If it's any use, for C, here it is:
http://cboard.cprogramming.com/showp...6&postcount=10
Although it doesn't use the use the timer mechanism.
--
Mats
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.
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
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.
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.
Not sure exactly what you mean...If you tell us exactly what you want to do may be able to give more detailed advice.
By creating custom handlers for the Timer event, you are subclassing.
Timer msgs are generated at set intervals, regardless of the state (complete/incomplete) of the previous timer event.
Consider a timer of 100 ms. Processing takes 90-110 ms. It is possible to still be processing one timer when the next fires. This will tend to make the unresponsive, as all it can do is process timers. [* Note1]
The things to remember/consider are;
Single Threaded:
Make sure a second timer event (of the same type) does not fire while processing a timer event. Killing the timer prior to processing and restarting the timer after processing ensures that the timer events do not collide. It means the interval between timer msgs becomes [Timer delay + processing time].
This may be undesirable in some situations.
Multi Threaded:
If the operation is lengthy you may want to launch a new thread each timer event.
This is because your app will be unresponsive to user input while the timer msg is being processed.
[Note1]
Timer msgs are the lowest priority in the msg queue. They will be ignored if other msgs are in the queue.
Only use WM_TIMER for non time critical processing (you don't mind an approx diff of +/-100 ms in each msg).
For time critical use QueryPerformanceTimer() or similar.