关闭

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

1037人阅读 评论(0) 收藏 举报

// 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;
}

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:884439次
    • 积分:10452
    • 等级:
    • 排名:第1583名
    • 原创:152篇
    • 转载:318篇
    • 译文:33篇
    • 评论:77条
    最新评论