[vc笔记]mfc启动线程,判断线程是否结束

//启动线程

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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值