zoukankan      html  css  js  c++  java
  • 使用PostThreadMessage (Using CWinThread) .

    .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);
          }
  • 相关阅读:
    nginx升级总结,漏洞升级
    【BUG解决】在git上pull时提示You have not concluded your merge. (MERGE_HEAD exists)
    如何自签名把http网站变成https网站(https自签名方法)
    ifly
    Shell排序和二叉树排序
    C/C++复习笔记(2)
    C/C++复习笔记(1)
    C语言字符串操作
    python+flask
    C语言的一点复习
  • 原文地址:https://www.cnblogs.com/lidabo/p/2814824.html
Copyright © 2011-2022 走看看