MFC中AfxBeginThread、AfxEndThread、GetExitCodeThread的配合使用(工作者線程)
//線程入口函數(全局) UINT MyThreadProc(LPVOID pParam) { //在需要添加返回值的地方 if (...) { AfxEndThread(0); return 0; } if (...) { AfxEndThread(1); return 1; } ... } //在主線程中創建線程,并獲取線程函數的返回值 CWinThread* pThread = AfxBeginThread(MyThreadProc, NULL, THREAD_PRIORITY_NORMAL, 0, 0, NULL); pThread->m_bAutoDelete = FALSE;//設置不自動釋放 pThread->ResumeThread();//恢復運行 WaitForSingleObject(pThread->m_hThread, -1);//等待運行結束 DWORD dwResult(0); BOOL flag = ::GetExitCodeThread(pThread->m_hThread, &dwResult);//執行成功返回TRUE,否則為FALSE. dwResult存儲線程函數的返回值 if (dwResult == 0) { ... } else if (dwResult == 1) { ... }
參考鏈接:
https://blog.csdn.net/qq_22034437/article/details/52711043
https://blog.csdn.net/xbmoxia/article/details/16985953

浙公網安備 33010602011771號