Differences between sendmessage and postmessage

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.