我用PostMesssage函数在实现线程间传递数据时,下面是我做的步骤,但是为什么消息响应函数不执行呢?在新建的线程(有CWinThread派生来)中,添加
         CAuthorityServerDlg *Dlg=new CAuthorityServerDlg();
hWnd=Dlg->hWnd;
         .......
         ::SendMessage(hWnd,MESSAGEGDATA,0,(LPARAM)ClientBuff);
         //Sleep(20);   //这句加不加都不输出
在头文件AuthorityServerDlg.h中定义#define MESSAGEGDATA WM_USER+1
         afx_msg void ReceiveData(WPARAM wParam,LPARAM lParam);在CAuthorityServerDlg类中添加
       ON_MESSAGE(MESSAGEGDATA,ReceiveData)       void CAuthorityServerDlg::ReceiveData(WPARAM wParam,LPARAM lParam)
      { 
MessageBox("refsre");
      }
但运行程序时为什么并没有MessageBox("refsre");的输出呀?

解决方案 »

  1.   

    线程间传递数据用PostThreadMessage
      

  2.   

    1.首先要说明的是,楼主发送的是窗口消息,而不是线程消息2. CAuthorityServerDlg *Dlg=new CAuthorityServerDlg();
    hWnd=Dlg->hWnd; 
    这两句代码之间有没有调用Create之类的函数创建窗口?
    不然,hWnd得到的是一个无效句柄
      

  3.   

    消息函数改为
    afx_msg LRESULT ReceiveData(WPARAM wParam,LPARAM lParam); LRESULT CAuthorityServerDlg::ReceiveData(WPARAM wParam,LPARAM lParam) 
          { 
    MessageBox("refsre"); 
    return 0;
          } 
      

  4.   

    CAuthorityServerDlg *Dlg=new CAuthorityServerDlg(); 
    之后必须调用Create函数创建窗口
      

  5.   

    用PostMesssage函数在实现线程间传递数据时,下面是我做的步骤,但是为什么消息响应函数不执行呢? 在新建的线程(有CWinThread派生来)中,添加 
            CAuthorityServerDlg *Dlg=new CAuthorityServerDlg(); 
    hWnd=Dlg->hWnd; 
            ....... 
            ::SendMessage(hWnd,MESSAGEGDATA,0,(LPARAM)ClientBuff); 
            //Sleep(20);  //这句加不加都不输出 
    在头文件AuthorityServerDlg.h中定义#define MESSAGEGDATA WM_USER+1 
            afx_msg void ReceiveData(WPARAM wParam,LPARAM lParam); 
      

  6.   

    CAuthorityServerDlg *Dlg=new CAuthorityServerDlg(); 
    hWnd=Dlg->hWnd; 
    ===========
    窗口出来了吗?在::SendMessage(hWnd,MESSAGEGDATA,0,(LPARAM)ClientBuff); 之前先判断一下hWnd是不是有效的吧!
      

  7.   

    得创建窗口呀,不然hWnd是无效的!
      

  8.   

    窗口还没生成,所以窗口句柄hWnd=Dlg->hWnd; 无效!