1.UI线程
2.工作线程
把Delphi里TThread的WaitFor函数转化成C++代码,就会是下面这个样子。
- BOOL TThread::WaitFor(HANDLE hThread)
- {
- MSG msg;
- HANDLE handle[1];
- handle[0] = hThread;
- DWORD dwWaitResult = 0;
- do
- {
- // This prevents a potential deadlock if the background thread
- // does a SendMessage to the foreground thread
- if (dwWaitResult == WAIT_OBJECT_0 + 1)
- PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE);
- dwWaitResult = MsgWaitForMultipleObjects(1, handle, false, 1000, QS_SENDMESSAGE);
- if (dwWaitResult == WAIT_FAILED)
- return FALSE;
- if (dwWaitResult == WAIT_TIMEOUT)
- {
- TerminateThread(hThread, 0);
- return FALSE;
- }
- }
- while (dwWaitResult != WAIT_OBJECT_0);
- return TRUE;
- }
使我疑惑的是这两行代码
- if (dwWaitResult == WAIT_OBJECT_0 + 1)
- PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE);
如果注释所讲是真的,那究竟在哪里处理了其它线程用SendMessage发送过来的消息呢?于是我翻MSDN,查PeekMessage函数,它有这样的解释:
“The PeekMessage function dispatches incoming sent messages, checks the thread message queue for a posted message, and retrieves the message (if any exist).”
我用的是MSND2005,好像再早些的版本里并无“dispatches incoming sent messages”这句。光看这样的注释,实在是太晦涩了些。想起我原来好像发过个讨论这问题的帖子,翻了下发帖记录,还真有,见http://topic.csdn.net/u/20070824/14/d0c96e50-6348-49d3-98e4-9fcc9ead5fdd.html。我当时找到的答案是这样的:
“找到答案了。答案在<<Programming Applications for Microsoft Windows>>第26章的Waking a Thread一节.
The Algorithm for Extracting Messages from a Thread ‘s Queue
...
1. If the QS_SENDMESSAGE flag is turned on, the system sends the message to the proper window procedure. Both the GetMessage and PeekMessage functions handle this processing internally and do not return to the thread after the window procedure has processed the message; instead, these functions sit and wait for another message to process.
我试着翻译一下:如果QS_SENDMESSAGE标志位有效,系统发送消息(即其它线程通过SendMessage发送过来的消息)到对应的窗口过程。GetMessage和PeekMessage函数在内部进行这样的处理,一直等到窗口过程处理完之后才会返回。也就是说,这些函数(GetMessage和PeekMessage)坐下来等到其它的消息处理完。”
书本里的解释已经很清楚详细了,我当时的译文有错误,原文是说GetMessage和PeekMessage调用窗口过程处理完SendMessage发送过来的消息后,还会继续坐下来等自己消息队列里的消息。
理论有了,结合着看看上面WaitFor的代码。MsgWaitForMultipleObjects的最后一个参数QS_SENDMESSAGE指明了如果其它有其它线程用SendMessage向本线程的窗口发送消息,MsgWaitForMultipleObjects就会立即返回,返回值为WAIT_OBJECT_0 + nCount(本例中nCount值为1),接下来就轮到PeekMessage登场了。
PeekMessage其实做了两件事,一件是把收到的消息标示为旧消息。见MSND里对MsgWaitForMultipleObjects返回值的解释:
“Functions such as PeekMessage, GetMessage, and WaitMessage mark messages in the queue as old messages.”
另一件就是把SendMessage发送过来的消息dispatch到窗口过程。下一次调用MsgWaitForMultipleObjects时队列里便没有新消息了。
那变旧的消息还在队列里吗?答案是不在。因为用SendMessage发送过来的消息根本就不会进应用程序的消息队列。在PeekMessage调用窗口过程处理完它时,它便消失了。而我们也很轻松的就知道,SendMessage函数把QS_SENDMESSAGE这个标志turn on了,PeekMessage和GetMessage还有WaitMessage把这个标志turn off了,把QS_SENDMESSAGE标志turn off的过程就是把消息变旧的过程。