Conclusion:

1. PostMessage cannot send the same message frequently unless the last PostMessage has been processed.

2. If using SendMessage results in a poor user experience for your application, check the message handler instead of simply changing it to PostMessage.

3. Do not use PostMessage if the message must be processed by the program. When the message queue is full, subsequent postMessages are discarded.

4. Use SendMessageTimeout if the program must process a message that may cause the program to freeze.

5. If the message is inconsequential, PostMessage is recommended.

6. For Windows-specific messages such as WM_HOTKEY, you can only use PostMessage (not described in this article).

——————— 

PostMessage simply queues messages, returns them regardless of whether other programs process them, and continues executing. This is an asynchronous message delivery function. SendMessage, which must wait for other programs to finish processing the message before returning, is a synchronous message drop function.

2. If a message is sent by PostMessage in the same thread, the message is first put into the message queue of the thread and then dispatched to the target window through the message cycle. When SendMessage sends a message, the system directly calls the message handler of the target window and returns the result. SendMessage sends messages in the same thread and does not enter the thread message queue.

SendMessage needs to be differentiated by the environment. If SendMessage is the window of this thread, it will not go through any message loop, nor put into the message queue, but directly call WindowProc, so GetMessage and PreTranslateMessage can not be captured. If SendMessage is sending a message to another thread or window of another process, the message goes into a message queue, and GetMessage and PreTranslateMessage can catch the message.

3. The return value of PostMessage indicates whether the PostMessage function is executed correctly. The return value of SendMessage represents the return value of another program that processed the message. I think you all know that.

4, if in a different thread. It works fine. SendMessage sends messages to the message queue of the thread that the target window belongs to, and then the thread that sent the message waits (in fact, it should still be doing some monitoring, Such as monitoring the QS_SENDMESSAGE flag), the thread that sent the message does not continue until the target window is finished processing and the result is returned.

The difference is obvious: SendMessage messages are not queued, while PostMessage messages are queued. It’s worth noting that although one is queued and the other is not, the message is ultimately processed in the same place: the system call window procedure (the recipient reacts).

——-

PostThreadMessage is used to pass messages between threads, either to the worker thread or to the UI thread. When sent to a worker thread, the worker thread must have its own message handler.

PostMessage is used to send to the UI thread and needs to specify a window handle.

The Windows message processing mechanism works like this: First, the system (that is, Windows) puts messages from hardware (mouse, keyboard, etc.) and messages from applications into a system message queue. And the application needs to have its own message queue, that is, the thread message queue, each thread has its own message queue, for multi-threaded applications have the same number of thread message queue. Windows message queue sends the message to the thread message queue. The thread message queue takes out one message at a time and sends it to the specified form, repeating the loop until the program exits. This loop relies on the message loop (while(GetMessage()) TranslateMessage(); DispatchMessage(); GetMessage() simply fetches a message from the thread message,TranslateMessage() converts a virtue key message to a character message, For example, VK_F1 is converted to WM_HELP and DispatchMessage sends the retrieved message to the destination window. If a WM_CLOSE message is received, the loop ends, postQIutMessage (0) is sent, and WM_DESTROY destroys the form!

 while (GetMessage(&msg, NULL, 0, 0))          //C++ code

{  

TranslateMessage(&msg);

DispatchMessage(&msg);

}