SendMessage vs PostMessage + WaitForSingleObject

我的梦境 提交于 2019-12-21 09:16:09

问题


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


回答1:


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.




回答2:


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.




回答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.




回答4:


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



来源:https://stackoverflow.com/questions/6779386/sendmessage-vs-postmessage-waitforsingleobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!