What is the difference between the SendMessage and PostMessage functions?
3 answers
PostMessage - Queues the message, and the code runs further.
SendMessage - The SendMessage function sends a message immediately.
(Refinement-commentary on the answer @Asen )
Both
PostMessageandSendMessage, if you use your terminology, “send a message right away,” because the only thing you can send messages to (with the exception of Nonqueued messages ) is in some internal queue .This is implemented as otherwise the solution of
race conditionslike "50 threads simultaneously sentSendMessage" would be very problematic.The most important thing to write about these functions is that
SendMessagewaits for the message to be processed and returns the result, whilePostMessagesimply puts the message into a queue and does not care what happens next.The latter fact, by the way, is noticeable from their signatures:
BOOL WINAPI PostMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); LRESULT WINAPI SendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
SendMessage is synchronous, PostMessage is asynchronous.