Solved

Okay to call KillTimer from OnTimer?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
cb: unreferenced local variable 11 72
How to programmatically differentiate between C and Java 10 188
What is sub-make ? 2 40
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

864 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now