[MFC]获取线程退出码

实例——获取线程退出码

1.添加两个CWinThread子类CMyThread1,CMyThread2;右击添加类-->选择MFC类-->类名:CMyThread1,CMyThread2;基类:CWinThread-->完成
// MyThread1.cpp : 实现文件
BOOL CMyThread1::InitInstance()
{
	// TODO: 在此执行任意逐线程初始化
	AfxEndThread(1000);//线程1的退出码
	return TRUE;
}

// MyThread2.cpp : 实现文件
BOOL MyThread2::InitInstance()
{
	// TODO: 在此执行任意逐线程初始化
	AfxEndThread(2000);//线程2的退出码
	return TRUE;
}
2.
CGetExitCodeThreadDlg::CGetExitCodeThreadDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CGetExitCodeThreadDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	//成员变量
	// 	CWinThread* pThread1;
	// 	CWinThread* pThread2;
	pThread1=NULL;
	pThread2=NULL;
}
CGetExitCodeThreadDlg::~CGetExitCodeThreadDlg()
{
	if (pThread1)
	{
		delete pThread1;
		pThread1=NULL;
	}
	if (pThread2)
	{
		delete pThread2;
		pThread2=NULL;
	}
}
//启动线程1按钮事件
void CGetExitCodeThreadDlg::OnBnClickedButtonStart1()
{ 
	if (pThread1)
	{
		delete pThread1;
		pThread1=NULL;
	}
	pThread1=AfxBeginThread(RUNTIME_CLASS(CMyThread1),THREAD_PRIORITY_ABOVE_NORMAL,0,CREATE_SUSPENDED);
	pThread1->m_bAutoDelete=FALSE;
	pThread1->ResumeThread();
}
//启动线程2按钮事件
void CGetExitCodeThreadDlg::OnBnClickedButtonStart2()
{ 
	if (pThread2)
	{
		delete pThread2;
		pThread2=NULL;
	}
	pThread2=AfxBeginThread(RUNTIME_CLASS(CMyThread2),THREAD_PRIORITY_ABOVE_NORMAL,0,CREATE_SUSPENDED);
	pThread2->m_bAutoDelete=FALSE;
	pThread2->ResumeThread();
}
//获取线程1结束码按钮事件
void CGetExitCodeThreadDlg::OnBnClickedButtonExitcode1()
{ 
	if (pThread1 == NULL)
	{
		return;
	}
	DWORD exitCode=0;
	if (::GetExitCodeThread(pThread1->m_hThread,&exitCode))
	{
		if (exitCode == STILL_ACTIVE)
		{
			AfxMessageBox(_T("线程1已经启动"));
		}
		else
		{
			CString strCode;
			strCode.Format(_T("退出码:%d"),exitCode);
			AfxMessageBox(strCode);
		}
	}
}
//获取线程2结束码按钮事件
void CGetExitCodeThreadDlg::OnBnClickedButtonExitcode2()
{ 
	if (pThread2 == NULL)
	{
		return;
	}
	DWORD exitCode=0;
	if (::GetExitCodeThread(pThread2->m_hThread,&exitCode))
	{
		if (exitCode == STILL_ACTIVE)
		{
			AfxMessageBox(_T("线程1已经启动"));
		}
		else
		{
			CString strCode;
			strCode.Format(_T("退出码:%d"),exitCode);
			AfxMessageBox(strCode);
		}
	}
}

3.结果图

 

posted @ 2013-04-28 19:18  javawebsoa  Views(351)  Comments(0Edit  收藏  举报