我可以暂停一个进程,除了一个线程? [英] Can I suspend a process except one thread?

查看:178
本文介绍了我可以暂停一个进程,除了一个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将暂停(或暂停)一个进程,除了一个线程。

I'm going to suspend(or pause) a process except one thread.

我试图使用SuspendThread(Api函数),结果是进程线程成为不负责任的状态。

I've tried to use SuspendThread(Api Function) and the result is process thread was became non-responsable status.

这不是我想要的。我想让简历一线,我必须做大的工作。

This is not what I wanted. I want to make resume one thread which I must do major work.

我该如何解决?请给出你的想法。

How can I solve this? Please give your idea.

谢谢。

推荐答案

CreateToolhelp32Snapshot 来获取属于进程的线程列表。一旦你有这个列表只是迭代,并暂停每个线程,不符合当前的线程ID。下面的示例是未经测试的,但应该可以正常工作。

You can call CreateToolhelp32Snapshot to get a list of threads belonging to a process. Once you have that list just iterate though it and suspend every thread that doesn't match the current thread ID. The example below is untested but should work just fine.

#include <windows.h>
#include <tlhelp32.h>

// Pass 0 as the targetProcessId to suspend threads in the current process
void DoSuspendThread(DWORD targetProcessId, DWORD targetThreadId)
{
    HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    if (h != INVALID_HANDLE_VALUE)
    {
        THREADENTRY32 te;
        te.dwSize = sizeof(te);
        if (Thread32First(h, &te))
        {
            do
            {
                if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID)) 
                {
                    // Suspend all threads EXCEPT the one we want to keep running
                    if(te.th32ThreadID != targetThreadId && te.th32OwnerProcessID == targetProcessId)
                    {
                        HANDLE thread = ::OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
                        if(thread != NULL)
                        {
                            SuspendThread(thread);
                            CloseHandle(thread);
                        }
                    }
                }
                te.dwSize = sizeof(te);
            } while (Thread32Next(h, &te));
        }
        CloseHandle(h);    
    }
}

这篇关于我可以暂停一个进程,除了一个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆