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);
          }
  • 相关阅读:
    Linux命令笔记
    拆功放板笔记
    从输入 URL 到页面加载完的过程中都发生了什么---优化
    python学习笔记(三)
    python学习笔记(二)
    python学习笔记(一)
    公交wifi运营平台分析
    testNG小试牛刀
    maven小项目注册服务(三)--web模块
    用maven进行测试
  • 原文地址:https://www.cnblogs.com/lidabo/p/2814824.html
Copyright © 2011-2022 走看看