开发者

How to suspend a running thread using win32 api's?

开发者 https://www.devze.com 2022-12-27 14:46 出处:网络
Here it is Create a thread in suspended state. hThrd1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadProc1, (LPVOID)

Here it is

  1. Create a thread in suspended state.

    hThrd1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadProc1, (LPVOID)
    &obj1, CREATE_SUSPENDED, &dwFirstThreadID);
    
  2. Resume the thread whenever required

    ResumeThread(hThrd1);
    

How do I suspend this running thread. I may resume it after sometime, but I want to suspend it now.

I 开发者_JS百科called

    SuspendThread(hThrd1);

Still the for loop in the ThreadProc keeps running. Now how do I avoid it? Also suggest me for any alternatives.


I got the problem. Initially thread HANDLE hThrd1 was declared inside WndProc. Since WndProc is being called again & again, the HANDLE that I got during CreateThread was not the same that was passed to suspend thread. (It was an embarrassing mistake)

Now I have declared it globally. This solves the problem and works as intended.

0

精彩评论

暂无评论...
验证码 换一张
取 消