SendMessage and PostMessage which one is better to use

message-queuemfcperformancesendmessagevisual studio 2010

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?

Best Answer

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.

Related Topic