Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, i'm writing a MFC C++ app. It contains animation effects(e.g. moving bitmaps). So i have an array that contains the coordinates of each shape(i'm using rectangles, ellipses). My code is simple
int y[100]; //the y-coordinate of each shape
Then in OnInitDialog() i use SetTimer(1, 100, NULL); And in OnTimer(UINT nIDEvent)
my code is:

for(int j=1; j<=100; j++)
{
if(y[j]>500)
{
KillTimer(j);
SetTimer(j+1, 100, NULL);
}
}

My point is that i'm destroying the timer event in OnTimer(UINT nIDEvent) and creating a new timer in the same function. Does MFC allow that activity and are there any problems doing that, such as memory leaks. Thanks!
Posted

Yes, it is possible to create a timer and kill it in OnTimer. This is useful for timers that fire only 1 time.
 
Share this answer
 
While it is possible, as it stands, it looks pointless to me.
:)
 
Share this answer
 
Nooop! It is not pointless... Don't listen these guys...

Yes you could kill your timer in the OnTimer implementation...

It can be useful when the timer itself wants to slow down or speedup himself...

It can be useful when a timer want to execute only a fix number of time for example... :doh:
 
Share this answer
 
"with an interval of just 100 milliseconds, there's a possibility that the timer will fire again before the tick handler even gets to your actual processing code."

No it will not fire again because we are necessary in the same thread and the next timer event will be fired only at the next message loop iteration... And at this time this will not occur because the timer wad already been destroyed...

From JSOP: Given that you (and we) don't have any idea how much code is being processed, or how it's being processed, and you're not the original poster, how can you possibly know this?
 
Share this answer
 
v2
If you want to find out what will happen, your best bet is to run the code under the debugger.

Beyond that, why are you destroying the timer and then recreating it again? That seems rather pointless to me. What you should be doing (barring the use of multi-threading) is turn the timer OFF instead of killing it), do your processing, and then turn it back on again. However, with an interval of just 100 milliseconds, there's a possibility that the timer will fire again before the tick handler even gets to your actual processing code.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900