car.cpp

来自「汽车线程多线程并行」· C++ 代码 · 共 63 行

CPP
63
字号
// Car.cpp: implementation of the CCar class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyThread.h"
#include "Car.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

extern CCriticalSection cs;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCar::CCar()
{

}
CCar::CCar(CPoint position,CPoint direction/* =CPoint(10 ,0)*/)
{
	m_CarPosition=position;
	m_CarOld=position;
	m_direction=direction;
	pDC=AfxGetApp()->GetMainWnd()->GetDC();
	pDC->TextOut(100,100,"fhdsajklfjks");
    m_pCarThread=AfxBeginThread(_CarMove,this,THREAD_PRIORITY_NORMAL);
}
CCar::~CCar()
{

}

UINT CCar::_CarMove(LPVOID lpparam)
{
	CCar* This=(CCar*)(lpparam);	//lpparam为构造器中传过来的"this"指针
	This->CarMove();
	return 0;
}

void CCar::CarMove()
{
	while(true)
	{
		cs.Lock();
		m_CarOld=m_CarPosition;
		m_CarPosition=m_CarPosition+m_direction;
		cs.Unlock();
		Sleep(500);
	}
}
void CCar::Start(CPoint position,CPoint direction/* =CPoint(10 ,0)*/)
{
    m_CarPosition=position;
	m_CarOld=position;
	m_direction=direction;
	pDC=AfxGetApp()->GetMainWnd()->GetDC();
	pDC->TextOut(m_CarPosition.x,m_CarPosition.y,"fhdsajklfjks");
    m_pCarThread=AfxBeginThread(_CarMove,this,THREAD_PRIORITY_NORMAL);
}

⌨️ 快捷键说明

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