//启动线程
void CMFCApplication1theadDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
m_pThread = nullptr;
//启动线程,把this作为参数,传递给线程函数
m_pThread = AfxBeginThread(LoadDict, this);
//获取该线程的结束码。
DWORD dwExitCode_dict = 0;
for (int i = 0; i < 5; i++)
{
Sleep(2000);
bool flag1 = GetExitCodeThread(m_pThread->m_hThread, &dwExitCode_dict);
if (dwExitCode_dict == STILL_ACTIVE)
{
CString str;
str.Format(_T("flag1 = %s \r\n"), flag1==true?L"true":L"false");
OutputDebugString(str);
}
}
}
声明全局线程函数
UINT LoadDict(LPVOID lpParam);
//线程函数
UINT LoadDict(LPVOID lpParam)
{
CMFCApplication1theadDlg* main = (CMFCApplication1theadDlg*)lpParam; //调用的类的定义
Sleep(8000);
//3:结束线程
AfxEndThread(11);
return 1;
}
//执行结果

当线程停止后,GetExitCodeThread的返回值立即变成false