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.