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);
      }
这是框架,要是需要什么操作,自己定义、添加自己的线程消息就可以了
DECLARE_DYNCREATE(CTestThread) 
IMPLEMENT_DYNCREATE(CTestThread, CWinThread)

支持 类的动态创建 相当于 从类的名字 可以直接创建类 RUNTIMECLASS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值