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

📄 childview.cpp

📁 模拟哲学家就餐过程
💻 CPP
字号:
// ChildView.cpp : implementation of the CChildView class

#include "stdafx.h"
#include "ThreadCompete.h"
#include "ChildView.h"

#undef  _ASSERTE
#define _ASSERTE(expr) ((void)0)
#include "Afxpriv.h"

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


/////////////////////////////////////////////////////////////////////////////
// CChildView
IMPLEMENT_DYNCREATE(CChildView, CFormView)

void CChildView::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDD_ABOUTBOX)
	{	//CAboutDlg dlgAbout;	dlgAbout.DoModal();
	}else{	CFormView::OnSysCommand(nID, lParam);	}
}

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CFormView::PreCreateWindow(cs))	return FALSE;
	return TRUE;
}

BOOL CChildView::OnEraseBkgnd(CDC* pDC)
{	 pDC->SetBkColor( RGB(250,250,250) );
	 return CFormView::OnEraseBkgnd(pDC);
}

BOOL CChildView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{	// TODO: Add your specialized code here and/or call the base class
	return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

CChildView::CChildView(): CFormView(CChildView::IDD){ bCanExit=1; counter=0;}

CChildView::~CChildView(){}

///////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CChildView,CFormView )
	//{{AFX_MSG_MAP(CChildView)
//	ON_WM_SYSCOMMAND()
	ON_WM_ERASEBKGND()
	ON_WM_CLOSE()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_BN_CLICKED(IDC_BLOCKSELF, OnBlockSelfChecked)
	ON_BN_CLICKED(IDC_BLOCKED, OnBlockChecked)
	ON_BN_CLICKED(IDC_SYNCHRONIZE, OnSyncChecked)
	ON_BN_CLICKED(IDC_SAVEBTN, OnSaveFileTXT)
	ON_BN_CLICKED(IDC_RETRYBTN, OnTheadCountChange)				
	ON_BN_CLICKED(IDC_CONTINOUSBTN, OnContinousPrompt)				

	ON_CBN_SELCHANGE(IDC_THREADCNTR, OnTheadCountChange)
	ON_CBN_SELCHANGE(IDC_THREADSTEP, OnTheadStepTimeChange)
	ON_CBN_SELCHANGE(IDC_PRIORITYCLASS, OnPriorityClassChange)
	ON_CBN_SELCHANGE(IDC_THREADPRIORITY, OnThreadPriorityChange)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_USER_NOTIFY, OnUserUpdateNotify)
	ON_MESSAGE_VOID(WM_INITIALUPDATE, OnInitialUpdate)
END_MESSAGE_MAP()

void CChildView::DoDataExchange(CDataExchange* pDX)
{	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CThreadForm)
	DDX_Control(pDX, IDC_FILENAME, m_EditBox);
	DDX_Control(pDX, IDC_DATABOX, m_ListBox);
	DDX_Control(pDX, IDC_THREADCNTR, m_Count);
	DDX_Control(pDX, IDC_THREADSTEP, m_steptime);
	DDX_Control(pDX, IDC_PAUSE, m_PauseCheck);
	DDX_Control(pDX, IDC_BLOCKSELF, m_BlockSelfChecked);
	DDX_Control(pDX, IDC_BLOCKED, m_BlockChecked);
	DDX_Control(pDX, IDC_SYNCHRONIZE, m_SyncChecked);
	DDX_Control(pDX, IDC_PRIORITYCLASS, m_ProcessPriority);
	DDX_Control(pDX, IDC_THREADPRIORITY, m_ThreadPriority);
	//}}AFX_DATA_MAP
}

void CChildView::OnClose()
{	 CFormView::OnClose(); }

void CChildView::OnDestroy()
{	if(pCollect) { 
		pCollect->DestroyThreadCollection();
		delete pCollect; pCollect=NULL;}  
	CFormView::OnDestroy();
}

/////////////////////////////////////////////////////////////////////////////

BOOL CChildView::OnUserUpdateNotify(WPARAM wParam, LPARAM lParam)
{	m_PauseCheck.SetCheck(1); m_Count.EnableWindow(1); m_steptime.EnableWindow(1);
	//CString message; message.Format("The %d persons to ending...", wParam);
	OnPaint();  Invalidate();  bCanExit=1;	return 1; //竞争结束后可以退出   
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	pCollect->DrawCollectionResult( &dc );
}

void CChildView::OnInitialUpdate() 
{	CFormView::OnInitialUpdate();
	pCollect = new CThreadCollection(); if(!pCollect)return;
	m_EditBox.SetWindowText( "ThreadCompete.txt" );
	m_SyncChecked.SetCheck(1);      m_PauseCheck.SetCheck(1);  
	m_Count.SetCurSel(0);           m_steptime.SetCurSel(2);
	m_ThreadPriority.SetCurSel(0);	m_ProcessPriority.SetCurSel(1);
	OnTheadCountChange(); //或者InitThreadCollection( 5, 50 );
}

/////////////////////////////////////////////////////////////////////////////
void CChildView::EnableDlgWindow(bool bEnable)
{
	CButton* pReset = (CButton*) GetDlgItem(IDC_RETRYBTN);//没有设置类变量邦定
	pReset->EnableWindow(bEnable);
	m_ProcessPriority.EnableWindow( bEnable );
	m_ThreadPriority.EnableWindow( bEnable );
	m_PauseCheck.EnableWindow( bEnable );
	m_SyncChecked.EnableWindow( bEnable );
	m_Count.EnableWindow( bEnable );
	m_steptime.EnableWindow( bEnable );
	if(pCollect)pCollect->bUpdateNotify = bEnable;
}

void CChildView::OnContinousPrompt()
{   CButton* pPrompt = (CButton*) GetDlgItem(IDC_CONTINOUSBTN);
    pPrompt->ShowWindow(SW_HIDE);
    if(pCollect)pCollect->PromptContinousCheckBox(SW_HIDE);
}

void CChildView::OnBlockSelfChecked()
{   bool bChecked = m_BlockSelfChecked.GetCheck()&3!=0;
	if(pCollect)pCollect->SetUnBlockSelfCheck(bChecked);
}

void CChildView::OnBlockChecked()
{   bool bChecked = m_BlockChecked.GetCheck()&3!=0;
	m_BlockSelfChecked.EnableWindow(bChecked);
	if(pCollect)pCollect->SetBlockCheckEnable(bChecked);
}

void CChildView::OnSyncChecked()
{   bool bChecked = m_SyncChecked.GetCheck()&3!=0;
	if(pCollect)pCollect->SetSyncCheckEnable(bChecked);
}

void CChildView::OnPriorityClassChange()
{	int nCurSel = m_ProcessPriority.GetCurSel();
	if(pCollect)pCollect->SetCurrentPriorityClass(nCurSel);
}

void CChildView::OnThreadPriorityChange()
{	int nCurSel = m_ThreadPriority.GetCurSel();
	if(pCollect)pCollect->SetPersonThreadsPriority( nCurSel );
}

void CChildView::OnTheadStepTimeChange()
{	CString  sTime;   m_steptime.GetWindowText(sTime); 
	if(pCollect)pCollect->SetupThreadStepTime( atoi(sTime) );
}

void CChildView::OnPause()
{   bool bPause = m_PauseCheck.GetCheck()&3!=0;
	m_Count.EnableWindow(bPause); m_steptime.EnableWindow(bPause);
	if(pCollect)pCollect->ToggleCollectionThreadsPause( bPause );
	OnPaint(); Invalidate(); if(!bPause)bCanExit=0; //正在竞争时不允许退出程序
}

void CChildView::OnTheadCountChange()
{	CString  sCount;
	EnableDlgWindow(false);
	bCanExit=1;  counter=0;							//初始可以退出
	m_PauseCheck.SetCheck(0);  
	if(pCollect){ 
		pCollect->DestroyThreadCollection();
		m_PauseCheck.SetCheck(1); OnPause();  
		m_Count.GetWindowText(sCount);  
		InitThreadCollection( atoi(sCount), 50 ); //处于挂起状态
	}
	EnableDlgWindow(true);
}

void CChildView::InitThreadCollection(int count, int delay)
{	//CString msg;
	m_PauseCheck.EnableWindow( false );
	while(m_ListBox.GetCount()>0)m_ListBox.DeleteString(0);
    //msg.Format("There are %d persons to beginging...", count);
	//int x = m_ListBox.AddString(msg);	 m_ListBox.SetCurSel(x);
	if(pCollect){
		pCollect->SetDisplayCoordinate(&CPoint(250,300), 180, 20);
		pCollect->initThreadCollectionClass( this, &m_ListBox, count , delay );
		pCollect->initCollectionThreads( m_ThreadPriority.GetCurSel() );	//处于挂起状态
	}
	//OnThreadPriorityChange();
	bCanExit=1;  OnPriorityClassChange();  
	OnSyncChecked(); OnBlockChecked(); OnBlockSelfChecked();
	m_PauseCheck.EnableWindow( true ); 
}

void CChildView::SaveListBoxToFile(CString filename)
{	int i, len; CString str;  CFile file;  CFileException e;

	if( file.Open(filename, CFile::modeCreate | CFile::modeWrite, &e) ){
		for (i=0; i<m_ListBox.GetCount(); i++)
		{	m_ListBox.GetText(i,str); str+="\r\n"; len=str.GetLength();
			file.Write(str,len);	}
		file.Close();
	}
}

void CChildView::OnSaveFileTXT()
{	CString filename;
	m_EditBox.GetWindowText(filename);
	SaveListBoxToFile(filename);
}

⌨️ 快捷键说明

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