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

上一篇:cond 使用介绍


下一篇:Linux - 线程