Solved

Need Help with an OnTimer Function

Posted on 2002-06-11
11
281 Views
Last Modified: 2012-08-13
i have an OnTimer function that checks for error messages and was just wondering what happens if the timer goes off and there is already a stack of functions to be executed.  Does it wait for the current one to finish, interrupt it, or add to the end of the queue?
0
Comment
Question by:hrasheed
[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
  • 6
  • 4
11 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 7070039
The message is not posted it is sent, i.e. it is not placed in a queue.  in other words, when the timer event occurs,t he window procedure is called and the message is processed.  No further timer events will then occur until the window procedure returns from the timer message.

thus if you are set to get timer messages every 1 second, but take 10 seconds to process them, you will in reality get messages at about 10 second intervals.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 7070055
Note that even though you are having a timer call-back procedure called, the system is still message based.  Windows will send a WM_TIMER message to your window.  Then if the timer message is passed to the default wndow procedure, it will call the specified call-back procedure.  

continues

0
 
LVL 22

Expert Comment

by:nietod
ID: 7070066
Note that there are (at least) 3 levels of priorities given to messages in the windows OS.

The WM_TIMER and WM_PAINT messages are given the lowest priorities.  (at least in their default uses).  In other words, all other messages that need to be handled are handled before these messages, noi matter how long they are
"waiting".  The reason for this is that they are not really placed in the message queue.  (At least not by the OS, a program could send them or post them in which case they would have higher priority).   Instead the GetMessage() (and other message functions) generate these messages only when the queue is empty and when the messages are needed (the timer has expired or the window has an update region).   Thus these messages never "stack up" in the queue.  This is important because they could potentially occur frequently and could A) cause the program to spend all of its time handling them repeatedly (which isn't necessary) and B) cause the queue to overflow (in win 95 and 98).

continues
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 7070073
Thus there is ordinarily no way to get a 2nd WM_TIMER message while processing a first one.  However, it could happen if durring the processing the 1st message, you get more messages.  i.e. if you start a 2nd message loop inside the processing of the 1st message.  This is unlikely, bit it might happen if you have a messagebox or modal dialog to show during the processing of the WM_TIMER event.    So this could happen, bit its under your control.  i..e. you would have to have code that allowed it to happen, in most cases it will never happen.
0
 

Author Comment

by:hrasheed
ID: 7070277
just to be sure, if say the program was handling a button click and the WM_TIMER comes in the middle, will the button click complete, or will the timer message be handled immediately
0
 
LVL 22

Expert Comment

by:nietod
ID: 7070452
It depends on what you mean by "handling a button click".

If you mean handling the final message produced by the button click, i.e the WM_LBUTTONUP for example, then you will not get the notification durring the handling of that message.

If you mean durring the processing of the mouse button sequence, well then it depends.   That  sequence is a series of messages that begin with the mopuse button press and that track the movement of the mouse and ends when the mouse is released.   in between these messages, other messages may occur, like WM_TIMER.  So yes it _could_ occur durring the process.  Does that matter?

Note that on a standard button control, all these messages are handled privately and I don-t think that the WM_TIMER message will ever "make it to you" during this.  the same with mouse interaction in a menu and moving/sizing a window.  I may be wrong about that.  However many messages are "aborted" durring these operations and I suspect this could be one of them.

if you handle the mouse sequnce yourself however, then the message could occur.


Why do you ask?   What are you doing that this is a concern?
0
 
LVL 30

Expert Comment

by:Axter
ID: 7070641
>>it could happen if durring the processing the 1st >>message, you get more messages
>>in most cases it will never happen.

Actually, I've had this problem happen in a number of applications.
The method I used to insure the call back function doesn't get call during current call back, is to add a static bool variable to the function, and check if it is set.

Example:

void SomeCallbackFunction(....)
{
   static bool Busy = false; //initiate to false
   if (!Busy)
   {
    Busy = true;
   //.. Your function code here
   
    Busy = false;
   }
}
0
 

Author Comment

by:hrasheed
ID: 7070661
i am writing a dialog box which can handle mouse clicks and keyboard input.  This box also has an item which should occur roughly on a given interval.  However, handling the mouse clicks (the functions are mapped to BN_CLICKED)  is more important that handling the timer item.  It is important that the timer either not interrupt the code handling the mouse click or that the dialog box return control to the code handling the mouse click in the correct location if it does.
0
 

Author Comment

by:hrasheed
ID: 7070664
i am writing a dialog box which can handle mouse clicks and keyboard input.  This box also has an item which should occur roughly on a given interval.  However, handling the mouse clicks (the functions are mapped to BN_CLICKED)  is more important that handling the timer item.  It is important that the timer either not interrupt the code handling the mouse click or that the dialog box return control to the code handling the mouse click in the correct location if it does.
0
 

Author Comment

by:hrasheed
ID: 7070690
when the shown SomeCallbackFunction returns, will it go back to complete handling the message it was processing when the second message interrupted it?
0
 
LVL 22

Expert Comment

by:nietod
ID: 7070779
>> Actually, I've had this problem happen in a number of applications.
This can only happend if you process messages during the WM_TIMER message.  That is unlikely, but possible.   However, messaget may be processed indirectly, like by opening a messagebox.  

>>  It is important that the timer
>> either not interrupt the code handling the
>> mouse click or that the dialog box return control to the
>> code handling the mouse click in the correct location if it does.
Well its always going to return control to the correct location.  There is no doubt about that.

The only way you can get a WM_TIMER message (and hence a call to your callback procedure) is when you (or code somewhere) calls GetMessage() or one of its alternatives.    So if in handling the the mouse, a private message loop is started--as is done by the default button procedure, then this private message loop will call GetMessage()  (or something similar) and it is possible that a WM_TIMER will occur and your callback will be called.  However, when this private message loop ends--like when the mouse button is release, then control will return to the location where the mouse processing started (somewhere inside a WM_LBUTTONDOWN handler, probalby inside the button's defautl window procedure.)    

 So program flow control will never bee "messed up" by this process.  However, you can have problems with rentrancy.  i..e if the WM_TIMER handler (includign the callback procedure, in your case) is not rentrant, this can be a problem.  So you need to look at what the handler does to make sure it is safely rentrant.  Things that might not be rentrant might be using unprotected global resources, like say using a global bitmap, opening a specific file, using a global data structure.  That sort of thing.   If you are doing anythign that makes the code non rentrant, then you need to protect against it.   Axter showed one technique for this, Its a good simple one.  there are others depending on your circumstances.  Like if you are in a multi-threaded environment, that is not a sufficient approach as you can have other reasons for rentrancy that it doesn't guard against.

>> when the shown SomeCallbackFunction returns, will it go back to
>> complete handling the message it was processing when the
>> second message interrupted it?
Absolutely!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

734 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