The difference between PostMessage, SendMessage and PeekMessage in VC + +

Source: Internet
Author: User
Tags function prototype getmessage message queue

1, PostMessage only put the message in the queue, regardless of whether the other program is processed to return, and then continue to execute, this is an asynchronous message delivery function. The SendMessage must wait for the other program to finish processing the message before returning, continue execution, this is a synchronous message delivery function. Also, the return value of PostMessage indicates that the PostMessage function was executed correctly, and the return value of SendMessage represents the return value of the message after the other program processed it. We should all understand this point.

2, if PostMessage sends a message within the same thread, the message is first placed in the thread's message queue and then dispatch through the message loop to the target window. When SendMessage sends a message, the system calls the target window's message handler directly and returns the result. SendMessage sends a message in the same thread and does not enter the thread message queue. If it is within a different thread. Better use PostThreadMessage instead of PostMessage, he works very well. SendMessage sends a message to the message queue of the thread that the destination window belongs to, and then waits for the thread to send the message (in fact, he should still be doing some monitoring work, such as monitoring the QS_SENDMESSAGE flag) until the target window finishes processing and the result is returned, and the line friend to send the message continues to run. This is the general case of SendMessage, in fact, the process is much more complex. For example, when the thread that sends the message detects a different window SendMessage a message arrives, he calls the window processing (re-entry) directly and returns the processing result (this process does not require support for getmessage in the message loop).

3, msdn:if you send a message with the range below Wm_user to the asynchronous message functions (PostMessage, Sendnotifyme Ssage, and Sendmessagecallback), its message parameters can not include pointers. Otherwise, the operation would fail.

If the message code being sent is below Wm_user (not a custom message) and the message parameter has pointers, then postmessage,sendnotifymessage,sendmessagecallback these asynchronous message send functions will fail. It is best not to use PostMessage to send messages with pointer parameters.

PostThreadMessage function function: This function puts a message into the message queue of the specified thread, and returns without waiting for the thread to process the message.
Function prototype: BOOL postthreadmessage (DWORD idthread,uint msg,wparam wparam,lparam iparam);
Parameters
Idthread: The thread identifier of the thread whose message will be sent. If the thread does not have Message Queuing, this function will fail. The system creates a message queue for a thread when the thread first calls a win or GDI function. To get more information, see Remarks.
MSG: Specifies the type of message that will be sent.
WParam: Specifies additional message-specific information.
Iparam: Specifies additional message-specific information.
Return value: Returns a value other than 0 if the function call succeeds. If the function call fails, the return value is zero. To get more error messages, call the GetLastError function. If Idthread is not a valid thread identifier or the thread determined by Idthread does not have a message team
Column, GetLastError returns ERROR_INVALID_THREAD.
Note: The thread that the message will be sent to must create a message queue, otherwise calling PostThreadMessage will fail. Use one of the following methods to handle this situation:
Call PostThreadMessage. If it fails, call sleep, call PostThreadMessage again, and execute repeatedly until PostThreadMessage succeeds.
Create an Event object, and then create the thread. Before calling PostThreadMessage, a function WaitForSingleObject is used to set the event to the known state. The message is sent to the thread called PeekMessage (£msg,null,wm_user,wm_user,pm_noremove) to force the system to create the message queue. Sets an event that indicates that the thread is ready to receive the message sent.
The thread that the message is sent to gets the message by calling Getmesssge or Peekmesssge. The HWND member in the returned MSG structure is null.

PeekMessage function Function: This function checks the thread message queue for a message and places the message (if it exists) in the specified structure.
Function prototype: BOOL peekmessage (lpmsg ipmsg,hwnd hwnd,uint wmsgfiltermin,uint wmsgfiltermax,uint wremovemsg);
Parameters:
LPMSG: MSG structure pointer that receives message information.
HWnd: The handle of the window whose message is being inspected.
Wmsgfiltermin: Specifies the first message in the range of messages being inspected.
Wmsgfiltermax: Specifies the last message in the range of messages being inspected.
Wremovemsg: Determines how messages are processed. This parameter is preferable to one of the following values:
After the pm_noremove:peekmessage is processed, the message is not removed from the queue.
After the pm_remove:peekmessage is processed, the message is removed from the queue.
The Pm_noyield can be combined arbitrarily into pm_noremove or pm_remove. This flag causes the system not to release threads waiting for the calling program to be idle.
By default, all types of messages are processed. To process only certain messages, specify one or more of the following values:
Pm_qs_input:windows NT5.0 and Windows 98: Handling mouse and keyboard messages.
Pm_qs_paint:windows NT 5.0 and Windows 98: Process paint messages.
Pm_qs_postmessage:windows NT 5.0 and Windows 98: Handles all messages sent, including timers and hotkeys.
Pm_qs_sendmessage:windows NT 5.0 and Windows 98: handles all send messages.
Return value: Returns a value other than 0 if the message is available, or zero if no message is available.
Note: Unlike the function getmessage, the function peekmesssge does not wait for the message to be placed in the queue until it is returned.
Peekmesssge only those messages that are associated with the window identified by the parameter HWND or that are lschild determined to be associated with their child windows, and the message is within the range determined by the parameters Wmsgfitermin and Wmsgfihermax. If the HWND is NULL, PeekMessage receives a message belonging to the window of the current calling thread (PeekMessage does not receive a message belonging to another thread's window). If the HWND is c1,peekmessage only returns a message with an HWND value of NULL, the message is sent by the function PostThreadMessage. If both Wmsgfiltermin and Wmsgfiltermax are zero, GetMessage returns all the available messages (that is, no range filtering).
Constants Wm_keyfirst and Wmkeylast can be used as filter values to get all the keyboard messages; constants Wm_mousefirst and Wm_mouselast are available to receive all mouse messages.
PeekMessage usually does not purge WM_PAINT messages from the queue. The message will remain in the queue until processing is complete. However, if the WM_PAINT message has an empty update area, PeekMessage clears the WM_PAINT message from the queue.
Windows CE: WM_PAINT messages that have a null update area are not purged from the queue.

The difference between PostMessage, SendMessage and PeekMessage in VC + +

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.