我想楼主是指的是“一个线程的消息队列实际上分为四种不同的消息队列:Post消息队列、Send消息队列、输入消息队列、应答消息队列。PostMessage是将消息追加到Post消息队列,SendMessage是追加到Send消息队列,两个队列处理的优先级并不一样”
这种提法并非Microsoft的提法,SDK文档中所说的消息队列应该是上面说的Post消息队列。关于SendMessage, MSDN有如下描述:
This function sends the specified message to a window or windows. SendMessage calls the window procedure for the specified window and does not return until the window procedure has processed the message. The PostMessage function, in contrast, posts a message to a thread's message queue and returns immediately.
If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message.
所以,要看你的“消息队列”是如何定义的了。
答:这个问题结合你的代码回答。
当不加AfxMessageBox("Just for try!");这条语句时:
你是说线程发的消息被堵塞了(当然了,因为你在线程中发送的消息需要传递到主UI线程的消息处理队列,才能被处理,而现在你的UI线程都在Sleep睡觉呢,根本就没去理会消息队列,若是你的Sleep里的时间很长,你会发现你的对话框界面死在那里,在进程管理器里显示“未响应”)。
好了,再来回到你说的,若是加了AfxMessageBox("Just for try!");这条语句,你好像发现新大陆似的,能够接收到消息,并且处理。那还用你说。在你弹出对话框时,操作系统的任务调度就把CPU时间片分给了你程序的其他线程myThread,他获得CPU时间,那么他就会发送消息,你点击按钮之后,同时在执行Sleep之前,主UI线程就会去处理myThread线程发送的消息。所以你会发现你的消息被处理了。