[MFC]线程优先级设置,下拉列表框ComboBox,复选框CButton,列表框CListBox的使用

实例——CountTestThreadMFC

1.CountTestThreadMFCDlg.cpp关键代码
#include "CountThread.h"
#include "DisplayThread.h"
BEGIN_MESSAGE_MAP(CCountTestThreadMFCDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_COMBO1, &CCountTestThreadMFCDlg::OnCbnSelchangeCombo1)
	ON_CBN_SELCHANGE(IDC_COMBO2, &CCountTestThreadMFCDlg::OnCbnSelchangeCombo2)
	ON_CBN_SELCHANGE(IDC_COMBO3, &CCountTestThreadMFCDlg::OnCbnSelchangeCombo3)
	ON_BN_CLICKED(IDC_CHECK3, &CCountTestThreadMFCDlg::OnBnClickedCheck3)
END_MESSAGE_MAP()

BOOL CCountTestThreadMFCDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	//定义的成员变量
	// 	CString mStrNamber;//线程所要操作的资源对象
	// 	CMutex* mMutex;//用于同步机制的互斥量
	// 	CWinThread* m_pCounterThread;//指向计数线程的指针
	// 	CWinThread* m_pDisplayThread;//指向显示线程的指针
	CComboBox* pBox;
	pBox=(CComboBox*)GetDlgItem(IDC_COMBO1);
	ASSERT(pBox != NULL);
	if (pBox != NULL)
	{
		pBox->AddString(L"Idle");
		pBox->AddString(L"Normal");
		pBox->AddString(L"High");
		pBox->AddString(L"Realtime");
		pBox->SetCurSel(1);
	}
	pBox=(CComboBox*)GetDlgItem(IDC_COMBO2);
	ASSERT(pBox != NULL);
	if (pBox != NULL)
	{
		pBox->AddString(L"Idle");
		pBox->AddString(L"Lowest");
		pBox->AddString(L"BelowNormal");
		pBox->AddString(L"Normal");
		pBox->AddString(L"AboveNormal");
		pBox->AddString(L"Highest");
		pBox->AddString(L"Timecritical"); 
		pBox->SetCurSel(3);
	}
	pBox=(CComboBox*)GetDlgItem(IDC_COMBO3);
	ASSERT(pBox != NULL);
	if (pBox != NULL)
	{
		pBox->AddString(L"Idle");
		pBox->AddString(L"Lowest");
		pBox->AddString(L"BelowNormal");
		pBox->AddString(L"Normal");
		pBox->AddString(L"AboveNormal");
		pBox->AddString(L"Highest");
		pBox->AddString(L"Timecritical");
		pBox->SetCurSel(3);
	}

	//初始化挂起复选框为挂起状态
	CButton* pBtn=(CButton*)GetDlgItem(IDC_CHECK3);
	pBtn->SetCheck(1);

	mStrNamber="0";
	mMutex=new CMutex(FALSE,NULL,NULL);
	//初始化线程
	CCountThread* pCountThread=new CCountThread;
	pCountThread->SetOwner(this);
	m_pCounterThread=pCountThread->myCreateThread();
	CDisplayThread* pDisplayThread=new CDisplayThread;
	pDisplayThread->SetOwner(this);
	m_pDisplayThread=pDisplayThread->myCreateThread();

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CCountTestThreadMFCDlg::OnPriorityChange(UINT nID)
{
	ASSERT(nID == IDC_COMBO2 || nID == IDC_COMBO3);
	CComboBox* pBox=(CComboBox*)GetDlgItem(nID);
	int nCurSel=pBox->GetCurSel();
	DWORD dw;
	switch (nCurSel)//工作线程的优先级有以下7种
	{
	case 0:dw=THREAD_PRIORITY_IDLE;break;
	case 1:dw=THREAD_PRIORITY_LOWEST;break;
	case 2:dw=THREAD_PRIORITY_BELOW_NORMAL;break;
	case 3:
	default: dw=THREAD_PRIORITY_NORMAL;break;
	case 4:dw=THREAD_PRIORITY_ABOVE_NORMAL;break;
	case 5:dw=THREAD_PRIORITY_HIGHEST;break;
	case 6:dw=THREAD_PRIORITY_TIME_CRITICAL;break;
	}
	if (nID == IDC_COMBO2)
	{
		m_pCounterThread->SetThreadPriority(dw);//调整计数线程优先级
	}
	else
	{
		m_pDisplayThread->SetThreadPriority(dw);//调整显示线程优先级
	}
}
//进程优先级下拉框选项已更改事件
void CCountTestThreadMFCDlg::OnCbnSelchangeCombo1()
{ 
	CComboBox* pBox=(CComboBox*)GetDlgItem(IDC_COMBO1);
	int nCurSel=pBox->GetCurSel();
	DWORD dw;
	switch (nCurSel)//界面线程的优先级有以下4种
	{
	case 0:	dw=IDLE_PRIORITY_CLASS;break;
	case 1:	
	default: dw=NORMAL_PRIORITY_CLASS;break;
	case 2:	dw=HIGH_PRIORITY_CLASS;break;
	case 3:	dw=REALTIME_PRIORITY_CLASS;break;
	}
	SetPriorityClass(GetCurrentProcess(),dw);//设置进程的优先级
}
//计数线程下拉框选项已更改事件
void CCountTestThreadMFCDlg::OnCbnSelchangeCombo2()
{ 
	OnPriorityChange(IDC_COMBO2);
}
//显示线程下拉框选项已更改事件
void CCountTestThreadMFCDlg::OnCbnSelchangeCombo3()
{ 
	OnPriorityChange(IDC_COMBO3);
}
//挂起复选框单击事件
void CCountTestThreadMFCDlg::OnBnClickedCheck3()
{ 
	CButton* pBtn=(CButton*)GetDlgItem(IDC_CHECK3);
    int state=pBtn->GetCheck(); 
	if (state == 1)//要改为挂起 
	{
		m_pCounterThread->SuspendThread();
		m_pDisplayThread->SuspendThread();
	}
	else
	{
		m_pCounterThread->ResumeThread();
		m_pDisplayThread->ResumeThread();
	} 
}
//向列表框中添加显示信息
void CCountTestThreadMFCDlg::AddToListBox(LPCTSTR szBuffer)
{
	CListBox* pListBox=(CListBox*)GetDlgItem(IDC_LIST_DATA);
	ASSERT(pListBox != NULL);
	if (pListBox != NULL)
	{
		int x=pListBox->AddString(szBuffer);
		pListBox->SetCurSel(x);
		if (pListBox->GetCount()>15)
		{
			pListBox->DeleteString(0);
		}
	}
}

2.BasicClass父类
//BasicClass.h 文件
#pragma once
#include "CountTestThreadMFCDlg.h"
class CCountTestThreadMFCDlg;
class BasicClass
{
public:
	BasicClass(void);
	virtual ~BasicClass(void);
	CCountTestThreadMFCDlg* mpCountTestThreadDlg; 
	void SetOwner(CCountTestThreadMFCDlg* Owner);
};
//BasicClass.cpp 文件
#include "StdAfx.h"
#include "BasicClass.h"
BasicClass::BasicClass(void)
{
	mpCountTestThreadDlg=NULL; 
}
BasicClass::~BasicClass(void)
{
}
void BasicClass::SetOwner(CCountTestThreadMFCDlg* Owner)
{
	mpCountTestThreadDlg=Owner;
}

3.CountThread子类
//CCountThread.h 文件
#pragma once
#include "BasicClass.h"
class CCountThread : public BasicClass
{
public:
	CCountThread(void);
	virtual ~CCountThread(void);
	CWinThread* myCreateThread();//创建线程计数
	friend UINT AddOne(LPVOID pParam);//加1计数
};
//CCountThread.h 文件
#include "StdAfx.h"
#include "CountThread.h" 
CCountThread::CCountThread(void)
{
}
CCountThread::~CCountThread(void)
{
}
CWinThread* CCountThread::myCreateThread()
{
	return AfxBeginThread(AddOne,(LPVOID)mpCountTestThreadDlg,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
}
UINT AddOne(LPVOID pParam)
{
	CCountTestThreadMFCDlg* mpCountTestThreadMFCDlg=(CCountTestThreadMFCDlg*)pParam;
	if (mpCountTestThreadMFCDlg == NULL)
	{
		return -1;
	}
	BOOL fSyncChecked;//线程同步机制复选检测
	unsigned int nNumber;//存储字符串中整数
	CSingleLock singleLock(mpCountTestThreadMFCDlg->mMutex);
	while(true)
	{
		fSyncChecked=mpCountTestThreadMFCDlg->IsDlgButtonChecked(IDC_CHECK1);
		if (fSyncChecked)
		{
			singleLock.Lock();
		}
		nNumber=_ttoi(mpCountTestThreadMFCDlg->mStrNamber);
		nNumber++;
		mpCountTestThreadMFCDlg->mStrNamber.Empty();
		mpCountTestThreadMFCDlg->mStrNamber.Format(_T("%d"),nNumber);
		if (mpCountTestThreadMFCDlg->IsDlgButtonChecked(IDC_CHECK2))
		{
			mpCountTestThreadMFCDlg->AddToListBox(_T("Counter:Add 1"));
		}
		Sleep(500);
		if (fSyncChecked)
		{
			singleLock.Unlock();
		}
	}
	mpCountTestThreadMFCDlg->PostMessageW(WM_CLOSE,0,0);
	return 0;
}

4.DisplayThread子类
//DisplayThread.h文件
#pragma once
#include "BasicClass.h"
class CDisplayThread :	public BasicClass
{
public:
	CDisplayThread(void);
	virtual ~CDisplayThread(void);
	CWinThread* myCreateThread();
	friend UINT Display(LPVOID pParam);
};
//DisplayThread.cpp文件
#include "StdAfx.h"
#include "DisplayThread.h" 
CDisplayThread::CDisplayThread(void)
{
}
CDisplayThread::~CDisplayThread(void)
{
}
CWinThread* CDisplayThread::myCreateThread()
{
	return AfxBeginThread(Display,mpCountTestThreadDlg,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
}
UINT Display(LPVOID pParam)
{
	CCountTestThreadMFCDlg* mpCounterThreadDlg=(CCountTestThreadMFCDlg*)pParam;
	ASSERT(mpCounterThreadDlg!=NULL);
	if (mpCounterThreadDlg == NULL)
	{
		return -1;
	}
	BOOL fSyncChecked;
	CString strBuffer;
	CSingleLock singleLock(mpCounterThreadDlg->mMutex);
	while (true)
	{
		fSyncChecked=mpCounterThreadDlg->IsDlgButtonChecked(IDC_CHECK1);
		if (fSyncChecked)
		{
			singleLock.Lock();
		}
		strBuffer=_T("Display:");
		strBuffer+= mpCounterThreadDlg->mStrNamber;
		mpCounterThreadDlg->AddToListBox(strBuffer);
		Sleep(1000);
		if (fSyncChecked)
		{
			singleLock.Unlock();
		}
	}
	mpCounterThreadDlg->PostMessageW(WM_CLOSE,0,0);
	return 0;
}

5.运行结果 [MFC]线程优先级设置,下拉列表框ComboBox,复选框CButton,列表框CListBox的使用_第1张图片

你可能感兴趣的:([MFC]线程优先级设置,下拉列表框ComboBox,复选框CButton,列表框CListBox的使用)