Posted on 2008-01-02 19:35
hongsion 閱讀(2063)
評論(5) 編輯 收藏 引用 所屬分類:
Windows
以下內(nèi)容完全把window 操作系統(tǒng)當作一個黑盒,因此所有內(nèi)容只能作為對其內(nèi)部的一個猜測。
1. windows操作系統(tǒng)內(nèi)部在創(chuàng)建一個線程的時候,會自動為它創(chuàng)建一個消息隊列。
2.每當一個線程創(chuàng)建一個窗口的時候,操作系統(tǒng)內(nèi)部都會把該窗口的Handle和線程相關(guān)聯(lián)。很有可能在操作系統(tǒng)內(nèi)部會維護一個窗口handle到線程的map. 還有一種可能就是窗口的成員變量里面有一個指針,指向創(chuàng)建它的線程。
3.窗口本身并沒有消息隊列,所有發(fā)到窗口的消息,都會自動被發(fā)到創(chuàng)建該窗口的線程的消息隊列中。
4.每個線程只能處理自己線程隊列里面的消息,不能處理其他線程消息隊列里面的消息。
所以PeekMessage(LPMSG lpMsg, HWND hWnd, UINT,UINT,UINT)函數(shù)中,如果hWnd不是本線程創(chuàng)建的窗口,則該函數(shù)調(diào)用失敗。
5.由于在線程消息隊列里面的消息會包含有窗口句柄,所以PeekMessage可以專門處理某個特殊窗口的消息。
6. 曾經(jīng)有疑問線程是不是只有創(chuàng)建了窗口才具有消息隊列,但又覺得應該不是這樣,因為在windows的API里面有個函數(shù)叫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", 這句話的意思是不是等同于創(chuàng)建一個窗口呢?