线程SuspendThread() ResumeThread()的使用

来源:互联网 发布:优酷网络连接失败 编辑:程序博客网 时间:2024/05/16 16:19

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();                 }        }}


1 0