[MFC]多任务线程

实例——多任务线程

其头文件关键代码:
// MultiTestThreadDlg.h : 头文件
#define WM_MSG WM_USER+1
class CMultiTestThreadDlg;
typedef struct THREAD_PARAM
{
	CMultiTestThreadDlg* pDlg;
	int nIndex;
}_THREAD_PARAM;
UINT ThreadFun(LPVOID pParam);
其cpp文件关键代码:
// MultiTestThreadDlg.cpp : 实现文件
CMultiTestThreadDlg::CMultiTestThreadDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMultiTestThreadDlg::IDD, pParent) 
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	pThread1=NULL;
	pThread2=NULL;
	mThreadParam.pDlg=NULL;
}  
void CMultiTestThreadDlg::DoDataExchange(CDataExchange* pDX)
{
	//成员变量
// 	CWinThread* pThread1;
// 	CWinThread* pThread2;
// 	THREAD_PARAM mThreadParam;
// 	CProgressCtrl mPgsCtrlOne;
// 	CProgressCtrl mPgrsCtrlTwo;
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PROGRESS_ONE, mPgsCtrlOne);
	DDX_Control(pDX, IDC_PROGRESS_TWO, mPgrsCtrlTwo);
}

BEGIN_MESSAGE_MAP(CMultiTestThreadDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_START1, &CMultiTestThreadDlg::OnBnClickedButtonStart1)
	ON_BN_CLICKED(IDC_BUTTON_START2, &CMultiTestThreadDlg::OnBnClickedButtonStart2)
	ON_BN_CLICKED(IDC_BUTTON_RESET, &CMultiTestThreadDlg::OnBnClickedButtonReset)
	//ON_MESSAGE(WM_MSG,&CMultiTestThreadDlg::OnMsgFun)//可以由virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);来替代
END_MESSAGE_MAP()


//重写CDialogEx::WindowProc( )消息函数
LRESULT CMultiTestThreadDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_MSG)
	{
		if (wParam==1)
		{
			mPgsCtrlOne.SetPos(lParam);
		}
		else if (wParam == 2)
		{
			mPgrsCtrlTwo.SetPos(lParam);
		}
	}
	return CDialogEx::WindowProc(message,wParam,lParam);//注意是CDialogEx
}
//WM_MSG消息函数,ON_MESSAGE(WM_MSG,&CMultiTestThreadDlg::OnMsgFun)
LRESULT CMultiTestThreadDlg::OnMsgFun(WPARAM wParam,LPARAM lParam)
{
	if (wParam==1)
	{
		mPgsCtrlOne.SetPos(lParam);
	}
	else if (wParam == 2)
	{
		mPgrsCtrlTwo.SetPos(lParam);
	}
	return 0;
}
//启动按钮1按钮
void CMultiTestThreadDlg::OnBnClickedButtonStart1()
{   
	if (pThread1 != NULL)
	{
		DWORD exitCode=0;
		if (::GetExitCodeThread(pThread1->m_hThread,&exitCode))
		{
			if (exitCode == STILL_ACTIVE)
			{
				AfxMessageBox(L"线程1已经启动。");
				return;
			} 
		}
	} 
	mThreadParam.nIndex=1;
	mThreadParam.pDlg=this;
	pThread1=AfxBeginThread(ThreadFun,&mThreadParam);
}
//启动线程2按钮
void CMultiTestThreadDlg::OnBnClickedButtonStart2()
{ 
	if (pThread2)
	{
		DWORD exitCode=0;
		if (::GetExitCodeThread(pThread2->m_hThread,&exitCode))
		{
			if (exitCode == STILL_ACTIVE)
			{
				AfxMessageBox(L"线程2已经启动。");
				return;
			} 
		}
	}
	mThreadParam.nIndex=2;
	mThreadParam.pDlg=this;
	pThread2=AfxBeginThread(ThreadFun,&mThreadParam);
	//pThread2->m_bAutoDelete=FALSE;//当为false时,要在析构函数中,进行WaitForSingleObject()删除线程
}
//重置按钮事件
void CMultiTestThreadDlg::OnBnClickedButtonReset()
{ 
	//终止线程
	if (pThread1)
	{
		TerminateThread(pThread1->m_hThread,NULL);
	}
	if (pThread2)
	{
		TerminateThread(pThread2->m_hThread,NULL);
	}
	//重置进度条
	mPgsCtrlOne.SetRange(0,100);
	mPgsCtrlOne.SetPos(0);
	mPgrsCtrlTwo.SetRange(0,100);
	mPgrsCtrlTwo.SetPos(0);
}
//线程函数,其格式是:UINT Fun(LPVOID pParam)
UINT ThreadFun(LPVOID pParam)
{
	THREAD_PARAM* pThreadParam=(THREAD_PARAM*)pParam;
	for (int i=0;i<1001;i++)
	{ 
		if (i%10==0)//是10的倍数时发送消息,更新对应的进度条
		{
			Sleep(1);//延缓时间
			::PostMessage(pThreadParam->pDlg->m_hWnd,WM_MSG,pThreadParam->nIndex,i/10); 
		}
	}
	return 0;
}
其结果:
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页