PostThreadMessage C ++(CWinThread)

时间:2015-09-01 15:50:54

标签: c++ multithreading mfc

我正在使用MFC C ++并且我正在尝试使用来自Dlg类的PostThreadMessage向CWinThread发送消息,并且该消息未在线程类上处理

.H文件:

   #define Message_Test_Id WM_USER + 1

   class CTestMsg : public CWinThread
   {
       DECLARE_DYNCREATE(CTestMsg)

       protected:
       CTestMsg();           // protected constructor used by dynamic        creation
       virtual ~CTestMsg();

   public:
       virtual BOOL InitInstance();
       virtual int ExitInstance();

   protected:
       afx_msg void OnTestMsg(WPARAM wParam, LPARAM lParam);//The Message
       DECLARE_MESSAGE_MAP()

};

线程的

.cpp:

   BEGIN_MESSAGE_MAP(CTestMsg, CWinThread)
       ON_THREAD_MESSAGE(Message_Test_Id,OnTestMsg)
   END_MESSAGE_MAP()

   ....
   void CTestMsg::OnTestMsg(WPARAM wParam, LPARAM lParam)
   {
     ...
   }

我正在尝试在Dlg类中发送消息:

   CTestMsg *m_testMsg = (CTestMsg*)AfxBeginThread(RUNTIME_CLASS(CTestMsg),THREAD_PRIORITY_NORMAL, 10000, CREATE_SUSPENDED, NULL);

       m_testMsg->PostThreadMessageW(Message_Test_Id, 0, 0);

为什么不处理邮件?谢谢! (抱歉我的英语不好)

1 个答案:

答案 0 :(得分:2)

您正在使用CREATE_SUSPENDED标志创建线程,要使其实际运行,您必须使用以下命令恢复它:

m_testMsg->ResumeThread();