Sending Messages

To send messages to a window, call the SendMessage function. Unlike PostMessage, SendMessage is a synchronous function. It does not return until the window procedure of the receiver window has processed the message.

You typically send a message when you want a window procedure to perform a task immediately. The SendMessage function sends the message directly to the window procedure of the receiving window. The SendMessage function waits until the window procedure completes processing and then returns the message result. Parent and child windows often communicate by sending messages to each other. For example, a parent window that has an edit control as its child window can set the text of the control by sending a message to it. The control can notify the parent window of user-initiated changes to the text by sending messages back to the parent.

If the receiving thread is the same as the sending thread, SendMessage calls the window procedure directly. If the receiving thread is a different thread from the sending thread, the two message queues synchronize the message passing. The sending thread does not continue executing until the receiving thread processes the message. The receiving thread does not process the message if it is not executing a message loop. Consequently, if you send a message to a window in a thread that is not executing a message loop, the sending thread stops responding.