11

I was wondering what's the difference between calling SendMessage (which blocks) and calling PostMessage in conjunction with WaitForSingleObject. Thoughts?

Filip Frącz
  • 5,881
  • 11
  • 45
  • 67

4 Answers4

19

SendMessage() may be denied, if you call it from within the context of a COM call, where the COM object lives in an STA (calls are dispatched through the message pump). PostMessage() is not restricted to adhere to COM apartment rules.

Also, PostMessage() puts the message on the end of the window's message queue. SendMessage() bypasses the queue. You can find a lengthier discussion on message queues on Raymond Chen's blog The Old New Thing.

My point is that there is more to the difference between SendMessage() and PostMessage() than meets the eye. I really recommend going through Raymond's blog, as he has covered many gotchas over the years.

Jörgen Sigvardsson
  • 4,839
  • 3
  • 28
  • 51
4

PostMessage and WaitForSingleObject allow you to do asynchronous messaging. You can send a message, do other things, and check back for a reply later. SendMessage is synchronous and requires you to wait.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • I guess I should have mentioned that my goal is to achieve synchronicity. I would do PostMessage and follow immediately with a call to WaitForSingleObject. – Filip Frącz Jul 21 '11 at 16:54
  • +1 for highlighting the main operational difference - PostMessaging an event and waiting on it allows the caller to continue processing. This can be useful if the goal is to wait at some later stage in the thread processing until a previous posted message has been completely handled in the GUI thread. – Martin James Jul 22 '11 at 10:24
3

I've always thought that SendMessage calls your windows procedure directly, skipping the message queue; while PostMessage just adds the message to the queue.

grayDad
  • 324
  • 1
  • 7
  • 3
    That depends on what thread SendMessage() is called from. Interthread calls are always marshalled through the pump, and not dispatched directly to the winproc. – Jörgen Sigvardsson Jul 21 '11 at 17:15
2

SendMessage is a single API call, therefore less prone to your errors. Go with the built-in rather than rolling your own.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • 3
    It's not that it's just less prone to errors. The functions serve a very different semantic purpose. Your choice to use one or the other should be based on what you wish to accomplish, not just how can I make this code "work". – Cody Gray - on strike Jul 22 '11 at 15:29
  • "Semantics" are for natural languages. Code is what code does, not what it's supposed to mean. – Seva Alekseyev Jul 22 '11 at 15:48
  • 1
    No, semantics for facilitating code readability, self-documentation, and adhering to API contracts. Just because code does something now doesn't mean it will always continue to do that same thing. And just because the implementer knows what the code does doesn't mean that the later maintainer will know why you did something in a completely non-obvious, unintuitive way. – Cody Gray - on strike Jul 22 '11 at 15:50
  • 4
    "Code is what code does, not what it's supposed to mean" The subject here is "SendMessage" and "PostMessage", which are symbolic names, which have semantics. Ignoring semantics and insisting on only considering the implementation is the most incompetent notion that a programmer could possibly have. – Jim Balter Jan 15 '14 at 19:16