后线程消息 C++ (CWinThread)

PostThreadMessage C++ (CWinThread)


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

.线程的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 中发送消息 class:

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

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

为什么消息没有被处理?谢谢! (抱歉我的英语不好)

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

m_testMsg->ResumeThread();