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

📄 msmqthread.cpp

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

#include "stdafx.h"
#include "resource.h"
#include "MSMQThread.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMSMQThread

IMPLEMENT_DYNCREATE(CMSMQThread, CWinThread)

CMSMQThread::CMSMQThread()
{
 m_pParentWnd=NULL;
 m_RunThread=NULL;
 m_ParentThread=NULL;
 m_HSingStepTimer=0;
 m_Killed=FALSE;
}

CMSMQThread::~CMSMQThread()
{
}

BOOL CMSMQThread::InitInstance()
{
	if(m_ParentThread !=NULL)
	   m_ParentThread->InitChildClass();//初始化子类
	m_HSingStepTimer=SetTimer(NULL,0,0,NULL);//10000是特殊事件部可用
	return TRUE;
}

int CMSMQThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
    if(m_ParentThread!=NULL) //如果子类对象指针不为空,调用子类对象的ExitThread(),以便线程退出时作释放操作
	 m_ParentThread->ExitThread();
	return CWinThread::ExitInstance();
}

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

/////////////////////////////////////////////////////////////////////////////
// CMSMQThread message handlers
void CMSMQThread::InitParam( CWnd *pWnd, DWORD dwMillionTime)
{
 // m_InQueuePath =InQPath;
  m_pParentWnd=pWnd;
}

//杀掉线程
void CMSMQThread::KillThread()
{
  if(m_RunThread==NULL)//说明是在运行线程中
  {
	m_Killed=TRUE; 
	PostThreadMessage(WM_QUIT,NULL,NULL);
  }
  else                //说明在继承类中
  {
	  m_RunThread->KillThread(); 
	  WaitForSingleObject(m_hThread, INFINITE);
      m_hThread=NULL;
      delete this;//释放创建者子类对象
  }
}

BOOL CMSMQThread::OnIdle(LONG lCount) 
{
	// TODO: Add your specialized code here and/or call the base class
   if(m_Killed==TRUE)//说明继承类调用了KillThread,此时关闭OnTimer,以便立即关闭线程
      KillTimer(NULL,m_HSingStepTimer);
	return CWinThread::OnIdle(lCount);
}



//消息
BOOL CMSMQThread::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message==WM_TIMER)
	{
	 if(pMsg->wParam==m_HSingStepTimer)
	 {
		 m_ParentThread->SingleStep(); //调用继承子类的SingStep
	 }
	 else //子类自己定义的OnTimer
	 {
	   if(m_ParentThread!=NULL)
         m_ParentThread->OnTimer(pMsg->wParam);
	 }
	}
	return CWinThread::PreTranslateMessage(pMsg);
}
//设置时间,返回值为EventID
UINT CMSMQThread::SetThreadTimer(UINT uElapse)
{
  UINT EventID;
  EventID=SetTimer(NULL,0,uElapse,NULL);
  return EventID;
}

BOOL CreateMSMQThread(CMSMQThread* pp, CWnd *pWnd, DWORD dwMillionTime)
{
 if(pp==NULL)
	 return FALSE;
 CMSMQThread* p1=(CMSMQThread*)::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;
}
//停止SingleStep
void CMSMQThread::StopSingStep()
{
  KillTimer(NULL,m_HSingStepTimer);
  m_HSingStepTimer=0;
}
//杀掉Timer事件
void CMSMQThread::KillThreadTimer(UINT EventID)
{
 KillTimer(NULL,EventID);
}

//开始SingStep()
void CMSMQThread::BeginSingStep()
{
 if(m_HSingStepTimer!=0)
	m_HSingStepTimer=SetTimer(NULL,0,0,NULL);
}

⌨️ 快捷键说明

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