Posted on 2008-01-02 19:35
hongsion 閱讀(2064)
評論(5) 編輯 收藏 引用 所屬分類:
Windows
以下內容完全把window 操作系統當作一個黑盒,因此所有內容只能作為對其內部的一個猜測。
1. windows操作系統內部在創建一個線程的時候,會自動為它創建一個消息隊列。
2.每當一個線程創建一個窗口的時候,操作系統內部都會把該窗口的Handle和線程相關聯。很有可能在操作系統內部會維護一個窗口handle到線程的map. 還有一種可能就是窗口的成員變量里面有一個指針,指向創建它的線程。
3.窗口本身并沒有消息隊列,所有發到窗口的消息,都會自動被發到創建該窗口的線程的消息隊列中。
4.每個線程只能處理自己線程隊列里面的消息,不能處理其他線程消息隊列里面的消息。
所以PeekMessage(LPMSG lpMsg, HWND hWnd, UINT,UINT,UINT)函數中,如果hWnd不是本線程創建的窗口,則該函數調用失敗。
5.由于在線程消息隊列里面的消息會包含有窗口句柄,所以PeekMessage可以專門處理某個特殊窗口的消息。
6. 曾經有疑問線程是不是只有創建了窗口才具有消息隊列,但又覺得應該不是這樣,因為在windows的API里面有個函數叫PostThreadMessage,可以直接把消息投遞到線程的消息隊列里面,而不需要任何窗口句柄。后來在MSDN里面有這么一段描述,覺得解釋的很詳細:
“The system maintains a single system message queue and one thread-specific message queue for each graphical user interface (GUI) thread. To avoid the overhead of creating a message queue for non–GUI threads, all threads are created initially without a message queue. The system creates a thread-specific message queue only when the thread makes its first call to one of the User or Windows Graphics Device Interface (GDI) functions".
這里唯一的疑問我想應該是”makes its first call to one of the User or Windows Graphics Device Interface (GDI) functions", 這句話的意思是不是等同于創建一個窗口呢?