消息线程,对MFC消息机制的局部模拟

// WorkerThread.h: interface for the CWorkerThread class.
//
//

#if !defined(AFX_WORKERTHREAD_H__E574C05E_5792_4A49_8021_6BBDD7E0C6DA__INCLUDED_)
#define AFX_WORKERTHREAD_H__E574C05E_5792_4A49_8021_6BBDD7E0C6DA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <list>

#define TWEN_ERROR_INVALID_MESSAGEID (~0)

#define TWEN_BEGIN_MESSAGE_MAP_LOCAL() LRESULT CWorkerThread::SendMessage(DWORD dwMsgID,WPARAM wParam,LPARAM lParam) /
{ /
 switch(dwMsgID) /
 {
#define TWEN_END_MESSAGE_MAP_LOCAL() default:return TWEN_ERROR_INVALID_MESSAGEID; /
 } /
}
/
#define TWEN_DECLARE_MESSAGE_MAP() virtual LRESULT SendMessage(DWORD dwMsgID,WPARAM wParam,LPARAM lParam);
#define TWEN_BEGIN_MESSAGE_MAP(CChild,CParent) LRESULT CChild::SendMessage(DWORD dwMsgID,WPARAM wParam,LPARAM lParam) /
{ /
 LRESULT lRet=CParent::SendMessage(dwMsgID,wParam,lParam); /
 switch(dwMsgID) /
 {
#define TWEN_END_MESSAGE_MAP() } /
 return lRet; /
}
#define TWEN_ON_MESSAGE(ID,OnID) case ID:return OnID(wParam,lParam);
#define TWEN_MSG
/
//system message begin
#define TWEN_TM_TEST  0
#define TWEN_TM_USER  1
//system message end

#ifndef _WINDEF_
#ifndef TWEN_WINDEF_
#define TWEN_WINDEF_
typedef unsigned long  DWORD;
typedef unsigned long  WPARAM;
typedef unsigned long  LPARAM;
typedef unsigned long  LRESULT;
typedef unsigned long  BOOL;
typedef unsigned long  UINT;
typedef void*    LPVOID;
#define TRUE    1
#define FALSE    0
#endif
#endif

class CWorkerThread 
{
public://构造和析构
 CWorkerThread();
 virtual ~CWorkerThread();
public://导出函数
 BOOL Start();//开始工作线程
 BOOL Stop();//停止工作线程
 virtual bool OnIdle();//返回false将直接导致线程结束,且不改变线程是否正在运行的状态指示
protected://内部函数
 static UINT ThreadLoop(LPVOID lpv);
 UINT m_MainLoop();
protected://消息响应函数
 //{{TWEN_MSG(CWorkerThread)
 LRESULT TWEN_MSG TWEN_OnTest(WPARAM wParam,LPARAM lParam);
 //}}TWEN_MSG
public://消息机制
 void PostMessage(DWORD dwMsgID,WPARAM wParam,LPARAM lParam);
 TWEN_DECLARE_MESSAGE_MAP()
protected://数据成员
 struct structItem
 {
  DWORD dwMsgID;
  WPARAM wParam;
  LPARAM lParam;
 };
 std::list<structItem> m_Items;
 bool m_bRun;
};

#endif // !defined(AFX_WORKERTHREAD_H__E574C05E_5792_4A49_8021_6BBDD7E0C6DA__INCLUDED_)


// WorkerThread.cpp: implementation of the CWorkerThread class.
//
//

#include "WorkerThread.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

CWorkerThread::CWorkerThread()
 :m_bRun(false)
{
}

CWorkerThread::~CWorkerThread()
{
}

void CWorkerThread::PostMessage(DWORD dwMsgID,WPARAM wParam,LPARAM lParam)
{
 structItem item;
 item.dwMsgID=dwMsgID;
 item.wParam=wParam;
 item.lParam=lParam;
 m_Items.push_back(item);
}

TWEN_BEGIN_MESSAGE_MAP_LOCAL()
 //{{TWEN_MSG_MAP_LOCAL(CWorkerThread)
 TWEN_ON_MESSAGE(TWEN_TM_TEST,TWEN_OnTest)
 //}}TWEN_MSG_MAP_LOCAL
TWEN_END_MESSAGE_MAP_LOCAL()

BOOL CWorkerThread::Start()
{
 //AfxBeginThread(CWorkerThread::ThreadLoop,this);
 m_bRun=true;
 return TRUE;
}
BOOL CWorkerThread::Stop()
{
 m_bRun=false;
 return TRUE;
}
bool CWorkerThread::OnIdle()
{
 //Sleep(100);
 return true;
}
UINT CWorkerThread::ThreadLoop(LPVOID lpv)
{
 return ((CWorkerThread*)lpv)->m_MainLoop();
}
UINT CWorkerThread::m_MainLoop()
{
 while(m_bRun)
 {
  if(m_Items.size()<1 || m_Items.empty())
  {
   if(OnIdle()) continue;
   else break;
  }
  structItem&item=m_Items.front();
  SendMessage(item.dwMsgID,item.wParam,item.lParam);
  m_Items.pop_front();
 }
 return 0;
}

LRESULT CWorkerThread::TWEN_OnTest(WPARAM wParam,LPARAM lParam)
{
 return 0;
}

 

//test.cpp

#include "WorkerThread.h"
#include "iostream.h"

#define TM_TEST TWEN_TM_USER+1

class CChild:public CWorkerThread
{
public:
 CChild();
 virtual ~CChild();
private:
 //{{TWEN_MSG(CChild)
 LRESULT TWEN_MSG OnTest(WPARAM wParam,LPARAM lParam);
 //}}TWEN_MSG
public:
 TWEN_DECLARE_MESSAGE_MAP()
};
CChild::CChild()
 :CWorkerThread()
{
}
CChild::~CChild()
{
}
TWEN_BEGIN_MESSAGE_MAP(CChild,CWorkerThread)
 //{{TWEN_MSG_MAP(CChild)
 TWEN_ON_MESSAGE(TM_TEST,OnTest)
 //}}TWEN_MSG_MAP
TWEN_END_MESSAGE_MAP()

LRESULT CChild::OnTest(WPARAM wParam,LPARAM lParam)
{
 cout<<"CChild::OnTest/n";
 return 0;
}

int main()
{
 CChild child;
 child.SendMessage(TM_TEST,0,0);
 cin.get();
 return 0;
}

//注意,并未完整测试,只是随便想想

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值