threads=AfxBeginThread(RUNTIME_CLASS(CMyThread));
在主线程中开启一个新的UI线程。在CMyThread中创建并且显示一个无模式对话框,
但是如何在主线程中或则其他的工作线程中来通知这个无模式对话框来“干活”呢?
我想用消息,但是不明白怎么发过去。没有这个无模式对话框的handle阿。

解决方案 »

  1.   

    BOOL PostThreadMessage(
      DWORD idThread, // thread identifier
      UINT Msg,       // message to post
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    );
    CWinThread* AfxBeginThread( CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );可以::PostThreadMessage(pWinThread->m_nThreadId,msg,wParam,lParam)
      

  2.   

    那么在Thread中如何接受阿?我看了一下,只有Run, PreTranslateMessage和OnCmdMsg,
    应该用哪一个呢?
      

  3.   

    我试了一下好像不行。
    我在msdn里面找到了。
    CWinThread::PostThreadMessage
    BOOL PostThreadMessage( UINT message , WPARAM wParam, LPARAM lParam );Return ValueNonzero if successful; otherwise 0.ParametersmessageID of the user-defined message.wParamFirst message parameter.lParamSecond message parameter.ResCalled to post a user-defined message to another CWinThread object. The posted message is mapped to the proper message handler by the message map macro ON_THREAD_MESSAGE.谢谢大家。
    不过还有一个问题请教---我呆会儿会加分的---就是:
      如何在主线程里面向这个UI线程传递参数?
    CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );CWinThread* AfxBeginThread( CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );创建工作线程的时候是可以的。
    但是不知道UI线程可不可以。
    或者如何在UI线程里面获取主线程中的数据?
      

  4.   

    CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );里面就有参数(LPVOID pParam)啊
      

  5.   

    过程:
    m_pSockListenThread=(CSockListenThread *)AfxBeginThread(RUNTIME_CLASS(CSockListenThread));//发消息,Para1携带参数
    m_pSockListenThread->PostThreadMessage(WM_STARTLISTEN,Para1,NULL);
    //UI县城的定义
    class CSockListenThread : public CWinThread
    {
    DECLARE_DYNCREATE(CSockListenThread)
    protected:
    CSockListenThread();           // protected constructor used by dynamic creation
    // Attributes
    public:
    // Operations
    public:
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CSockListenThread)
    public:
    virtual BOOL InitInstance();
    virtual int ExitInstance();
    //}}AFX_VIRTUAL// Implementation
    protected:
    virtual ~CSockListenThread(); // Generated message map functions
    //{{AFX_MSG(CSockListenThread)
    // NOTE - the ClassWizard will add and remove member functions here.
    afx_msg void OnEndSockThread(WPARAM wParam,LPARAM lParam);
             //用来接收消息WM_STARTLISTEN
            afx_msg BOOL OnStartListen(WPARAM wParam,LPARAM lParam
            //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };//消息的映射:
    BEGIN_MESSAGE_MAP(CSockListenThread, CWinThread)
    //{{AFX_MSG_MAP(CSockListenThread)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    ON_THREAD_MESSAGE(WM_ENDSOCKTHREAD,OnEndSockThread)
                      //OnStartListen是消息WM_STARTLISTEN的处理函数
    ON_THREAD_MESSAGE(WM_STARTLISTEN,OnStartListen)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    //消息处理函数
    BOOL CSockListenThread::OnStartListen(WPARAM wParam,LPARAM lParam)
    {
    //wParam即为携带的参数
             
    return TRUE;
    }
      

  6.   

    把UI窗口句柄做为参数传递给工作线程,然后工作线程使用 PostMessage 发消息给 UI,注意 最好不要使用SendMessage.