⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mobasethread.cpp

📁 一个手机通信的源代码 一个手机通信的源代码一个手机通信的源代码
💻 CPP
字号:
// MoBaseThread.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"	
#include "MoBaseThread.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMoBaseThread

IMPLEMENT_DYNCREATE(CMoBaseThread, CWinThread)

CMoBaseThread::CMoBaseThread()
{
   m_ParentThread=NULL;
   m_RunThread=NULL;

}

CMoBaseThread::~CMoBaseThread()
{
}

BOOL CMoBaseThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	if(m_ParentThread !=NULL)
	   m_ParentThread->InitChildClass();//初始化子类
	return TRUE;
}

int CMoBaseThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	if(m_ParentThread !=NULL)
	   m_ParentThread->ExitThread();
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CMoBaseThread, CWinThread)
	//{{AFX_MSG_MAP(CMoBaseThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMoBaseThread message handlers
void CMoBaseThread::InitParam(CWnd *pWnd, DWORD dwMillionTime)
{
  m_pParentWnd=pWnd;
}
void CMoBaseThread::KillThread()
{
 if(m_RunThread==NULL)//说明是在运行线程中
  {

	PostThreadMessage(WM_QUIT,NULL,NULL);
  }
  else                //说明在继承类中
  {
	  m_RunThread->KillThread(); 
	  WaitForSingleObject(m_hThread, INFINITE);
      m_hThread=NULL;
      delete this;//释放创建者子类对象
  }
}
//设置时间,返回值为EventID
UINT CMoBaseThread::SetThreadTimer(UINT uElapse)
{
  UINT EventID;
  EventID=SetTimer(NULL,0,uElapse,NULL);
  return EventID;
}

BOOL CMoBaseThread::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message==WM_TIMER)
	{
	  if(m_ParentThread!=NULL)
         m_ParentThread->OnTimer(pMsg->wParam);
	 
	}
	return CWinThread::PreTranslateMessage(pMsg);
}
//创建一个对象
BOOL CreateMoThread(CMoBaseThread* pp, CWnd *pWnd, DWORD dwMillionTime)
{
 if(pp==NULL)
	 return FALSE;
 CMoBaseThread* p1=(CMoBaseThread*)::AfxBeginThread(pp->GetRuntimeClass(),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
 p1->m_ParentThread=pp;  //记住创建该线程的子类对象
 pp->m_RunThread=p1;     //创建者子类记住运行线程对象
 pp->m_hThread=p1->m_hThread; 
 p1->InitParam(pWnd,dwMillionTime);
 pp->InitParam(pWnd,dwMillionTime);
 p1->ResumeThread();
 return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -