Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Is it a good practice for a window to post messages to itself?
If a window sends a message to itself, doesn't that mean that the window procedure gets called recursively?

I am getting confused by the description of SendMessage(): "The function calls the window procedure for the specified window and does not return until the window procedure has processed the message". If this is happening within the same window procedure, the entire thread must be one huge sequence of recursive calls.
Posted
Updated 12-Oct-11 8:49am
v2

1 solution

It's like this: When a program sends a message to it's own window this is a totally valid operation. Next time the dispatcher checks in the messge loop if there are messages to process, the WindowsProc is called with the corresponding parameters. Recursion that will make your program stop working will only happen if the same message that was received would be resent to its own window. You'll have to be very wary of what you are doing as the recursion might take more than one step:

Received Message | Sent Message
-------------------------------
       A         |      B
       B         |      C
       C         |      A


You can easily spot the loop occuring when any message A, B or C was sent to the windows WndProc.

Best Regards,

—MRB
 
Share this answer
 
Comments
Espen Harlinn 12-Oct-11 14:49pm    
Good point :)
Manfred Rudolf Bihy 12-Oct-11 14:59pm    
Thanks Espen! 5+
Sergey Alexandrovich Kryukov 12-Oct-11 22:51pm    
Great explanation, my 5. However, OP could figure out that there is no recursion simply because there is no a function call to a message handler from a message handler.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900