Solved

Okay to call KillTimer from OnTimer?

Posted on 2006-07-14
4
902 Views
Last Modified: 2012-05-05
void CBlah::OnTimer( UINT nIDEvent )
{
    case( nIDEvent )
    {
        ...
     
        case BLAH_TIMER:
        KillTimer( BLAH_TIMER );
        //do stuff
        break;
       ...
    }
}

I'm not sure it's kosher. Would you know if there is any potential problem with doing this?

Thank you !!
Sandra
0
Comment
Question by:aquarius003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 100 total points
ID: 17111122
That's fine
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 100 total points
ID: 17111912
OnTimer() is an event handler that gets called when your program receives a Windows message (WM_TIMER) from the system that a timer has fired. It is not directly connected to the timer itself, so it is perfectly safe to do any timer manipulation you want from within the event handler, including killing it.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
ID: 17113752
>>>> Would you know if there is any potential problem with doing this?

Yes, I would know. But as rstaveley and wayside already told there is none. Killing a timer simply removes the timer from the schedule list what is absolutely non-critical for the timer evently handling currently in progress.

Just to add to above comments: timer events were generated and handled in the main (windows) message loop. AFAIK, timer messages only get processed at idle times. So timer events are not very accurate. If you need a trustful periodical timer you need to care that any other event handling, e. g. mouse move or button click were handled shortly cause a lengthy event handling of other events definitively would prevent the message loop to call the timer proc in time.

Regards, Alex
0
 
LVL 7

Accepted Solution

by:
nafis_devlpr earned 119 total points
ID: 17116928
By seeing your code it seems that you want to do something just once, thats why you created a timer and as soon the timer message got processed you just kill the timer, well actually theres no problem with killing a timer inside a OnTimer message, actually its done often if you just have to do run some piece of code for a certain time and after the clock reaches that time you just shut down the clock.

However, the timer message WM_TIMER got a very low priority, so if you try to do something critical with time I would recommend to use a thread that sleeps for a while and then do the job and then sleeps again for a while in a loop, in this way you will be able to do jobs periodically without missing a single clock tick, but if you want idle time processing then you can use WM_TIMER, ofcourse there are other ways to do idle time processing.

But killing a timer inside OnTimer function, there is no problem

Nafis
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question