1. The first difference is the meaning of the returned value. The declaration in msdn:
Lresult sendmessage (
HwndHwnd,
UintMSG,
WparamWparam,
LparamLparam
);
Bool postmessage (
HwndHwnd,
UintMSG,
WparamWparam,
LparamLparam
);
The four parameters have the same meaning and different return value types (in fact, from the data point of view, they are a 32-bit number, but they have different meanings ), lresult indicates the returned value after the message is processed, and bool indicates whether the message is successfully post.
2. postmessage is asynchronous and sendmessage is synchronous.
Postmessage only puts the message into the queue. no matter whether the message is processed or not, the message may not be processed. sendmessage is returned only after the message is processed. if the message is not processed, the thread that sends messages will be blocked all the time.
3. When sendmessage is sent in the same thread, the user32.dll module calls the message processing program in the target window and returns the result. Sendmessage sends messages in the same thread but not in the thread message queue. When a postmessage sends a message, the message must first be placed in the thread's message queue, and then distributed to the target window (dispatchmessage) cyclically through the message ).
If sendmessage is sent to the Message Queue of the thread to which the target window belongs within different threads, the thread that sends the message monitors and waits for message processing in the user32.dll module until the target window finishes processing and returns the message. Sendmessage does a lot of work before it returns, for example, it responds to other threads to sendmessage. When post to another thread, it is best to use postthreadmessage instead of postmessage.HwndThe parameter can be null, which is equivalent to postthreadmessage + getcurrentthreadid. Post wm_quit should be replaced by postquitmessage.