线程SuspendThread() ResumeThread()的使用

SuspendThread():挂起线程 If the function succeeds, the return value is the thread's previous suspend count; otherwise, it is (DWORD) -1.

ResumeThread():启动线程  If the function succeeds, the return value is the thread's previous suspend count; otherwise, it is (DWORD) -1.

If the return value is zero, the specified thread was not suspended. If the return value is 1, the specified thread was suspended but was restarted.

If the return value is greater than 1, the specified thread is still suspended.

在满足条件时启动线程,不满足条件时挂起线程

CWinThread *pthread;
创建线程
pthread=AfxBeginThread(ThreadProc,(LPVOID)this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL)

if(condition)
{
        DWORD r=pthread->ResumeThread();
        if(r==-1)
            "启动线程失败"
}
else
{//挂起线程
        DWORD suspendcount=pthread->SuspendThread();
        if(suspendcount==-1)
             "挂起线程失败"
        else
        {
                for (DWORD i = 0; i < suspendcount; i++)//如果挂起多次,需要启动多余挂起的次数,保证只挂起一次,否则满足条件时线程并不能启动
                {
                     pthread->ResumeThread();
                 }
        }
}


  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值