[转贴] 使用PostThreadMessage (Using CWinThread)

    技术2022-05-20  51

    .h 文件 #define WM_TEST    WM_USER + 1 class CTestThread : public CWinThread {     DECLARE_DYNCREATE(CTestThread) protected:     CTestThread ();            virtual ~CTestThread (); public:     virtual BOOL InitInstance();     virtual int  ExitInstance(); protected:     afx_msg void OnTest(WPARAM wParam,LPARAM lParam);     DECLARE_MESSAGE_MAP() }; .Cpp 文件 #include "stdafx.h" #include "TestThread.h" IMPLEMENT_DYNCREATE(CTestThread, CWinThread) CTestThread::CTestThread() { } CTestThread::~CTestThread() { } BEGIN_MESSAGE_MAP(CTestThread, CWinThread)     ON_THREAD_MESSAGE(WM_TEST,OnTest) END_MESSAGE_MAP() BOOL CTestThread::InitInstance() {         return TRUE; } int CTestThread::ExitInstance() {     return CWinThread::ExitInstance(); } void CTestThread::OnTest(WPARAM wParam,LPARAM lParam) {     AfxMessageBox("test"); } 调用的地方     CWinThread* m_pThrd;        //启动        m_pThrd = AfxBeginThread(RUNTIME_CLASS(CTestThread));              // 需要执行线程中的操作时         m_pThrd->PostThreadMessage(WM_TEST,NULL,NULL);             // 结束线程        HANDLE hp=m_pThrd->m_hThread;       if (hp)       {         if (WaitForSingleObject(hp, 1) != WAIT_OBJECT_0)         {             TerminateThread(hp,0);         }         CloseHandle(hp);       }


    最新回复(0)