What is the difference between the SendMessage and PostMessage functions?

    3 answers 3

    PostMessage - Queues the message, and the code runs further.

    SendMessage - The SendMessage function sends a message immediately.

      (Refinement-commentary on the answer @Asen )

      • Both PostMessage and SendMessage , 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 conditions like "50 threads simultaneously sent SendMessage " would be very problematic.

      • The most important thing to write about these functions is that SendMessage waits for the message to be processed and returns the result, while PostMessage simply 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.