Solved

Okay to call KillTimer from OnTimer?

Posted on 2006-07-14
4
925 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

690 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