📄 elevatorthread.cpp
字号:
// ElevatorThread.cpp : 实现文件
//
#include "stdafx.h"
#include "Elevators.h"
#include "ElevatorThread.h"
#include "ElevatorsDlg.h"
#include "CommonInc.h"
// CElevatorThread
UINT Runself(LPVOID pParam)
{
int Current_f;
CElevatorThread* p_Thread=(CElevatorThread*)pParam;
int Elevator_ID=p_Thread->Elevator_ID;
CElevatorsDlg* temp=(CElevatorsDlg*) theApp.m_pMainWnd;
while(!theApp.WaitQueue[Elevator_ID-1].empty())
{
Current_f=theApp.WaitQueue[Elevator_ID-1].front();
if(Current_f==theApp.Current_Floors[Elevator_ID-1])
{
theApp.WaitQueue[Elevator_ID-1].pop_front();
if (p_Thread->last_state==UP_RUN)
{
if(theApp.WaitQueue[Elevator_ID-1].empty())
{
theApp.Lift_States[Elevator_ID-1]=IDLE;
temp->Color_The_Button(Current_f,theApp.Lift_States[Elevator_ID-1],p_Thread->last_state,FALSE);
}
else
{
theApp.Lift_States[Elevator_ID-1]=UP_PAUSE;
temp->Color_The_Button(Current_f,theApp.Lift_States[Elevator_ID-1],p_Thread->last_state,FALSE);
}
temp->Elevator_IDLE(Elevator_ID,theApp.Current_Floors[Elevator_ID-1],FALSE);
p_Thread->PopDlg();
temp->Elevator_IDLE(Elevator_ID,theApp.Current_Floors[Elevator_ID-1],TRUE);
}
else if(p_Thread->last_state==DOWN_RUN)
{
if(theApp.WaitQueue[Elevator_ID-1].empty())
{
theApp.Lift_States[Elevator_ID-1]=IDLE;
temp->Color_The_Button(Current_f,theApp.Lift_States[Elevator_ID-1],DOWN_PAUSE,FALSE);
}
else
{
theApp.Lift_States[Elevator_ID-1]=DOWN_PAUSE;
temp->Color_The_Button(Current_f,theApp.Lift_States[Elevator_ID-1],DOWN_PAUSE,FALSE);
}
temp->Elevator_IDLE(Elevator_ID,theApp.Current_Floors[Elevator_ID-1],FALSE);
p_Thread->PopDlg();
temp->Elevator_IDLE(Elevator_ID,theApp.Current_Floors[Elevator_ID-1],TRUE);
}
}
while(Current_f!=theApp.Current_Floors[Elevator_ID-1])
{
if(theApp.Lift_States[Elevator_ID-1]==IDLE)
{
if (Current_f>theApp.Current_Floors[Elevator_ID-1])
{
theApp.Lift_States[Elevator_ID-1]=UP_RUN;
p_Thread->last_state=UP_RUN;
}
else if(Current_f<theApp.Current_Floors[Elevator_ID-1])
{
theApp.Lift_States[Elevator_ID-1]=DOWN_RUN;
p_Thread->last_state=DOWN_RUN;
}
}
else if(theApp.Lift_States[Elevator_ID-1]==UP_RUN||theApp.Lift_States[Elevator_ID-1]==UP_PAUSE)
{
theApp.m_pMainWnd->SendMessage(WM_USER+110,Elevator_ID,0);
Sleep(800);
theApp.Current_Floors[Elevator_ID-1]++;
}
else if(theApp.Lift_States[Elevator_ID-1]==DOWN_RUN||theApp.Lift_States[Elevator_ID-1]==DOWN_PAUSE)
{
theApp.m_pMainWnd->SendMessage(WM_USER+110,Elevator_ID,0);
Sleep(800);
theApp.Current_Floors[Elevator_ID-1]--;
}
Current_f=theApp.WaitQueue[Elevator_ID-1].front();
}
}
theApp.Lift_States[Elevator_ID-1]=IDLE;
temp->Elevator_IDLE(Elevator_ID,theApp.Current_Floors[Elevator_ID-1],FALSE);
temp->Color_The_Button(theApp.Current_Floors[Elevator_ID-1],theApp.Lift_States[Elevator_ID-1],p_Thread->last_state,FALSE);
return NULL;
}
IMPLEMENT_DYNCREATE(CElevatorThread, CWinThread)
CElevatorThread::CElevatorThread()
{
}
CElevatorThread::~CElevatorThread()
{
}
BOOL CElevatorThread::InitInstance()
{
// TODO: 在此执行任意逐线程初始化
return TRUE;
}
int CElevatorThread::ExitInstance()
{
// TODO: 在此执行任意逐线程清理
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CElevatorThread, CWinThread)
END_MESSAGE_MAP()
// CElevatorThread 消息处理程序
////////////弹出对话框
void CElevatorThread::PopDlg()
{
switch(Elevator_ID)
{
case 1: Dlg_Pop=new CElevatorControl_A;break;
case 2: Dlg_Pop=new CElevatorControl_B;break;
case 3: Dlg_Pop=new CElevatorControl_C;break;
case 4: Dlg_Pop=new CElevatorControl_D;break;
case 5: Dlg_Pop=new CElevatorControl_E;break;
}
pop=TRUE;
CString temp;
temp.Format(L"%d",theApp.Current_Floors[Elevator_ID-1]);
Dlg_Pop->pThread=this;
INT_PTR nResponse =Dlg_Pop->DoModal();
if (nResponse == IDOK)
{
// TODO: 在此处放置处理何时用“确定”来关闭
// 对话框的代码
}
else if (nResponse == IDCANCEL)
{
// TODO: 在此放置处理何时用“取消”来关闭
// 对话框的代码
}
}
/////////////////////////对话框消息函数
void CElevatorThread::On_Bk_Buttons(UINT Target_ID)
{
if(((int)(Target_ID-1090)<theApp.Current_Floors[Elevator_ID-1])&&(theApp.Lift_States[Elevator_ID-1]==UP_PAUSE))//||theApp.Lift_States[Elevator_ID-1]==IDLE))
{
::AfxMessageBox(L"这不是向下的电梯!!",MB_OK);
}
else if(((int)(Target_ID-1090)>theApp.Current_Floors[Elevator_ID-1])&&(theApp.Lift_States[Elevator_ID-1]==DOWN_PAUSE))//||(theApp.Lift_States[Elevator_ID-1]==IDLE)
{
::AfxMessageBox(L"这不是向上的电梯!!",MB_OK);
}else if((int)(Target_ID-1090)==theApp.Current_Floors[Elevator_ID-1])
{
::AfxMessageBox(L"这是目的地!!",MB_OK);
}else
{
if(theApp.WaitQueue[Elevator_ID-1].empty())
{
if(theApp.Current_Floors[Elevator_ID-1]>(int)(Target_ID-1090))
Add_To_Queue(Target_ID-1090,DOWN_RUN,FALSE);
else if(theApp.Current_Floors[Elevator_ID-1]<(int)(Target_ID-1090))
Add_To_Queue(Target_ID-1090,UP_RUN,FALSE);
}
else
switch(theApp.Lift_States[Elevator_ID-1])
{
case IDLE :
//AfxBeginThread(Runself,(LPVOID)this);
break;
case DOWN_PAUSE:
Add_To_Queue(Target_ID-1090,DOWN_RUN,FALSE);
break;
case UP_PAUSE:
Add_To_Queue(Target_ID-1090,UP_RUN,FALSE);
break;
}
}
}
void CElevatorThread::Add_To_Queue(int Target_Floor,int state,bool run)
{
if(theApp.WaitQueue[Elevator_ID-1].empty())
{
if(theApp.Current_Floors[Elevator_ID-1]==1)
if(Target_Floor>=theApp.Current_Floors[Elevator_ID-1])
{
theApp.Lift_States[Elevator_ID-1]=UP_RUN;
last_state=UP_RUN;
}
else if(Target_Floor<theApp.Current_Floors[Elevator_ID-1])
{
theApp.Lift_States[Elevator_ID-1]=DOWN_RUN;
last_state=DOWN_RUN;
}
else if(theApp.Current_Floors[Elevator_ID-1]!=1)
theApp.Lift_States[Elevator_ID-1]=state;
theApp.WaitQueue[Elevator_ID-1].push_back(Target_Floor);
theApp.WaitQueue[Elevator_ID-1].unique();
if(run)
AfxBeginThread(Runself,(LPVOID)this);
}
else
{
theApp.WaitQueue[Elevator_ID-1].push_back(Target_Floor);
switch(state)
{
case UP_RUN:
theApp.WaitQueue[Elevator_ID-1].sort(less<int>());
theApp.WaitQueue[Elevator_ID-1].unique();
restart=TRUE;
break;
case DOWN_RUN:
theApp.WaitQueue[Elevator_ID-1].sort(greater<int>());
theApp.WaitQueue[Elevator_ID-1].unique();
restart=TRUE;
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -