请问关于在CWinThread的派生类里注册消息的有关问题

请教关于在CWinThread的派生类里注册消息的问题
请问:
1、CWinThread的派生类能不能用ON_MESSAGE等方法注册消息?就是做到像对话框那样能够SendMessage、PostMessage,并在其对应的线程执行时进入处理消息函数
2、如果1可以的话,能不能做到在其线程执行到某个临界区时,如果消息到来,则丢弃消息。临界区过了,就接收新的消息
3、如果1可以的话,能不能做到在其线程执行到某个临界区时,如果消息到来,则把消息入队。临界区过了,就依次处理队列里的消息,并开始接受新的消息

另:给出思路

------解决方案--------------------
1、ON_THREAD_MESSAGE( message, memberFxn ),但这不叫注册!叫映射!据说用
ON_MESSAGE( message, memberFxn )也可以。

2、3、不是很明白你的意思,“线程执行到某个临界区”?怎么执行到?无非是消息处理函数里或者空闲处理里!作为CWinThread的派生类不可能做到丢弃消息等等。但你可以在消息处理函数里使用TryEnterCriticalSection,如果不能获得访问权就直接返回。


------解决方案--------------------
看MSDN里的说明
There are two general types of threads that CWinThread supports: worker threads and user-interface threads. Worker threads have no message pump: for example, a thread that performs background calculations in a spreadsheet application. User-interface threads have a message pump and process messages received from the system. CWinApp and classes derived from it are examples of user-interface threads. Other user-interface threads can also be derived directly from CWinThread.

UI线程 
PeekMessage
PM_NOREMOVE Messages are not removed from the queue after processing by PeekMessage. 
PM_REMOVE Messages are removed from the queue after processing by PeekMessage. 

------解决方案--------------------
如果是界面线程的话,你可以看成为一个你的主窗口(主线程)一样的东西,可以一样处理...........

我原来的意思是:希望做到的框架是线程一执行就循环执行程序,消息到来时就暂停循环执行的程序进入消息处理,消息处理完了以后再继续执行循环执行的程序。这能实现吗?

当然可以这样,只要在循环里面加上消息处理循环就可以了...getmessage...translatemessage...dispatchmessage.........
------解决方案--------------------
while(!b)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
{
switch(msg.message)
{
case WM_QUIT:
b = true;
break;
case WM_LBUTTONDOWN:
break;

}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
fun();//没有消息就处理。。
}
------解决方案--------------------
如果是界面线程的话,你可以看成为一个你的主窗口(主线程)一样的东西,可以一样处理...........

我原来的意思是:希望做到的框架是线程一执行就循环执行程序,消息到来时就暂停循环执行的程序进入消息处理,消息处理完了以后再继续执行循环执行的程序。这能实现吗?

当然可以这样,只要在循环里面加上消息处理循环就可以了...getmessage...translatemessage...dispatchmessage.........
------解决方案--------------------
PostThreadMessage()这个
-----------------------------------------
详细参考MSDN
CWinThread::PostThreadMessage
See Also Send Feedback
 

Updated: November 2007

Called to post a user-defined message to another CWinThread object. 

 
BOOL PostThreadMessage(
UINT message ,
WPARAM wParam,
LPARAM lParam 
);
 

Parameters
message
ID of the user-defined message. 

wParam
First message parameter. 

lParam
Second message parameter. 

Return Value
Nonzero if successful; otherwise 0. 

Remarks
The posted message is mapped to the proper message handler by the message map macro ON_THREAD_MESSAGE. 

Note: 
When calling the Windows PostThreadMessage function within an MFC application, the MFC message handlers are not called. For more information, see the Knowledge Base article, "PRB: MFC Message Handler Not Called with PostThreadMessage()" (Q142415).