Solved

Okay to call KillTimer from OnTimer?

Posted on 2006-07-14
4
844 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
Comment Utility
That's fine
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 100 total points
Comment Utility
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
Comment Utility
>>>> 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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++.

762 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

11 Experts available now in Live!

Get 1:1 Help Now