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

解决方案 »

  1.   

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

  2.   

    看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. 
      

  3.   

    To2楼:
        你给的资料很好。要接收消息的话只能用user-interface thread了
        那用user-interface thread能不能做到:启动时循环执行一系列程序,消息到来时暂停循环执行的程序然后处理消息?To1楼:
        我原来的意思是:希望做到的框架是线程一执行就循环执行程序,消息到来时就暂停循环执行的程序进入消息处理,消息处理完了以后再继续执行循环执行的程序。这能实现吗?
      

  4.   

    如果是界面线程的话,你可以看成为一个你的主窗口(主线程)一样的东西,可以一样处理...........我原来的意思是:希望做到的框架是线程一执行就循环执行程序,消息到来时就暂停循环执行的程序进入消息处理,消息处理完了以后再继续执行循环执行的程序。这能实现吗?当然可以这样,只要在循环里面加上消息处理循环就可以了...getmessage...translatemessage...dispatchmessage.........
      

  5.   

     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();//没有消息就处理。。
    }
      

  6.   

    如果是界面线程的话,你可以看成为一个你的主窗口(主线程)一样的东西,可以一样处理...........我原来的意思是:希望做到的框架是线程一执行就循环执行程序,消息到来时就暂停循环执行的程序进入消息处理,消息处理完了以后再继续执行循环执行的程序。这能实现吗?当然可以这样,只要在循环里面加上消息处理循环就可以了...getmessage...translatemessage...dispatchmessage.........
      

  7.   

    PostThreadMessage()这个
    -----------------------------------------
    详细参考MSDN
    CWinThread::PostThreadMessage  
    See Also  Send Feedback  
     Updated: November 2007Called 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. Res
    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). 
     
      

  8.   

    5楼的方法:
        如果fun()里的程序用时很长的话,能做到有消息进来的时候,暂停fun()的执行,消息处理完以后,再继续执行fun()(从刚才暂停的地方开始执行)吗?
      

  9.   

    1是可以的?
    LZ能给我个例子吗, 或者链接, 我找了好久没找到类似的,  谢谢了。
    [email protected]