19

searching the web for the difference between these two function calls, I've found that:

The difference between these two API calls is the way that they return control to the calling application. With SendMessage control is not returned to the calling application until the window that the message was sent to has completed processing the sent message, however with PostMessage control is returned to the calling application immediately, regardless of weather or not the sent message has been processes.

postMessage: Sends a message in the message queue associated with the thread and returns without waiting for the thread to process that messaage.

SendMessage: calls the window procedure for the specified window and does not return until the window procedure has processed the message.

PostMessage is a Asynchronous function where as SendMessage is a synchronous function.

Now the question is for my application ( which needs high performance ) which function is better to be used?

Note that I'm using MFC in visual studio 2010 and for this code:

CWnd *pParentOfClass = CWnd::GetParent();
pParentOfClass ->  

I just have these functions for sending messsages:

  • PostMessageW

  • SendMessage

  • SendMessageW

More questions:
Can you tell me the difference between SendMessage and SendMessageW?

Sepideh Abadpour
  • 2,550
  • 10
  • 52
  • 88

1 Answers1

22

You actually already answered your own question by describing SendMessage and PostMessage.

SendMessage: Sends a message and waits until the procedure which is responsible for the message finishes and returns.

PostMessage: Sends a message to the message queue and returns immediately. But you don't know when that message is actually being processed. Therefore, if you should be expecting an answer from that processed message, you will most likely get it through a message as well.

It really depends which one to use, but the end results are pretty much the same, it's just about timing. Also, PostMessage is especially useful in multi-threaded applications, which allows you to safely communicate between threads via their created windows.

PostMessage or SendMessage that end with A or W are just indicators how strings will be interpreted, i.e. single or multibyte, respectively. The ones without the A or W ending are preprocessor macros and will delegate to whatever you're application is set up to.

bcause
  • 1,303
  • 12
  • 29
  • can you explain this part more? **The ones without the A or W ending are preprocessor macros and will delegate to whatever you're application is set up to.** – Sepideh Abadpour Aug 30 '13 at 08:27
  • Shouldn't be too important, just use the generic ones (i.e. without special endings). See [ANSI/Unicode in WinAPI](http://msdn.microsoft.com/en-us/library/windows/desktop/dd374089%28v=vs.85%29.aspx) for more info. – bcause Aug 30 '13 at 08:34
  • Infact @no_seriously, I understood from your answer that if I expect the handler to return a value and do my process based on that value, I should use SendMessage but the main question is which one has better performance. A part of your answer says **it's just about timing** Could you explain it a little more? – Sepideh Abadpour Aug 30 '13 at 08:47
  • 2
    Well, taking it very strictly, I'd say SendMessage is "faster" since it doesn't have to go through the message queue and gets processed right away. PostMessage delays the processing until the messages, which were in the queue before the message was posted, were processed. So to make it simple and satisfying, **SendMessage is faster**. But once again, that shouldn't be the main concern when talking about high performance, it all depends on what exactly you want to archive. – bcause Aug 30 '13 at 08:54
  • 4
    @sepideh If you are asking about a performance comparison between two API calls that do different things, you are doing it wrong. – IInspectable Aug 30 '13 at 08:55