Solved

Okay to call KillTimer from OnTimer?

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

827 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