windows 检查 Win32 线程是否正在运行或处于挂起状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1006691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a Win32 thread is running or in a suspended state
提问by Canopus
How do I check to see if a Win32 thread is running or in suspended state?
如何检查 Win32 线程是否正在运行或处于挂起状态?
I can't find any Win32 API which gives the state of a thread. So how do I get the thread state?
我找不到任何提供线程状态的 Win32 API。那么如何获取线程状态呢?
采纳答案by Chris Becke
I think - originally - this information was not provided because any API that provided this info would be misleading and useless.
我认为 - 最初 - 未提供此信息,因为提供此信息的任何 API 都会产生误导和无用。
Consider two possible cases - the current thread has suspended the thread-of-interest. Code in the current thread knowsabout the suspended state and should be able to share it so theres no need for the kernel team to add an API.
考虑两种可能的情况 - 当前线程已挂起感兴趣的线程。当前线程中的代码知道挂起状态并且应该能够共享它,因此内核团队无需添加 API。
The 2nd case, some other / a 3rd thread in the system has suspended the thread of interest (and theres no way to track which thread that was). Now you have a race condition - that other thread could, at any time - unsuspend the thread of interest and the information gleaned from the API is useless - you have a value indicating the thread is suspended when it is in fact, not.
第二种情况,系统中的其他/第三个线程已挂起感兴趣的线程(并且无法跟踪是哪个线程)。现在你有一个竞争条件 - 其他线程可以随时 - 取消挂起感兴趣的线程,从 API 收集的信息是无用的 - 你有一个值表明线程实际上是挂起的,而不是。
Moral of the story - if you want to know that a thread is suspended - suspend it: The return value from SuspendThread is the previous suspend count of the thread. And now you DO know something useful - The thread WAS AND STILL IS suspended - which is useful. Or that it WASN't (but now is) suspended. Either way, the thread's state is now deterministically known so you can in theory make some intelligent choices based on that - whether to ResumeThread, or keep it suspended.
这个故事的寓意——如果你想知道一个线程被挂起——挂起它: SuspendThread 的返回值是线程的前一个挂起计数。现在您确实知道了一些有用的东西 - 线程 WAS AND STILL IS 挂起 - 这很有用。或者它没有(但现在)被暂停。无论哪种方式,线程的状态现在都是确定性已知的,因此理论上您可以基于此做出一些明智的选择 - 是 ResumeThread 还是保持挂起状态。
回答by Stephen Kellett
You can get this information by calling NtQuerySystemInformation() with the value for SystemProcessesAndThreadsInformation (integer value 5).
您可以通过使用 SystemProcessesAndThreadsInformation 的值(整数值 5)调用 NtQuerySystemInformation() 来获取此信息。
If you want an example of what you can do with this information take a look at Thread Status Monitor.
如果您想要一个可以使用此信息做什么的示例,请查看线程状态监视器。
回答by Richard
WMI's Win32_Threadclass has a ThreadState
property, where 5: "Suspended Blocked" and 6:Suspended Ready.
WMI 的Win32_Thread类有一个ThreadState
属性,其中 5:“Suspended Blocked”和 6:Suspended Ready。
You will need the Thread's Id to get the right instance directly (the WMI object's Handle property is the thread id).
您将需要线程的 ID 来直接获取正确的实例(WMI 对象的 Handle 属性是线程 ID)。
EDIT: Given this PowerShell query:
编辑:鉴于此 PowerShell 查询:
gwmi win32_thread | group ThreadState
gives
给
Count Name Group ----- ---- ----- 6 2 {, , , ...} 966 5 {, , , ...}
WMI has a different definition of "Suspended" to Win32.
WMI 对“暂停”的定义与 Win32 不同。
回答by Andrey
you could get thread suspend count with code like this:
您可以使用如下代码获取线程挂起计数:
DWORD GetThreadSuspendCount(HANDLE hThread) {
DWORD dwSuspendCount = SuspendThread(hThread);
ResumeThread(hThread);
return dwSuspendCount;
}
but, as already said - it is not accurate. Moreover, suspending a thread is evil.
但是,正如已经说过的 - 它不准确。此外,暂停线程是邪恶的。
回答by Brian R. Bondy
In Windows 7, you can use QueryUmsThreadInformation. (UMS stands for User mode scheduling).
在 Windows 7 中,您可以使用QueryUmsThreadInformation。(UMS 代表用户模式调度)。
See herefor UmsThreadIsSuspended
.
见这里的UmsThreadIsSuspended
。
回答by anand
I think the state here is referred to as
我认为这里的状态被称为
- If thread is in thread proc , doing some processing Or
- Waiting for event
- 如果线程在线程 proc 中,做一些处理或者
- 等待活动
This can be taken care of by using variable which can tell that if a thread is actually running or waiting for event to happen.
这可以通过使用变量来处理,该变量可以判断线程是否正在实际运行或等待事件发生。
These scenarios appear when considering threadpools, having some n threads and based on each thread running status , tasks can be assigned to idle threads.
这些场景在考虑线程池时出现,有一些 n 个线程,并且基于每个线程的运行状态,可以将任务分配给空闲线程。
回答by Elmue
YES: it IS possible to get the thread state and determine if it is suspended.
YES:可以获取线程状态并确定它是否被挂起。
And NO: You don't need Windows 7 to do that.
不:你不需要 Windows 7 来做到这一点。
I published my working class here on Stackoverflow: How to get thread state (e.g. suspended), memory + CPU usage, start time, priority, etc
我在 Stackoverflow 上发布了我的工作课程:如何获取线程状态(例如挂起)、内存 + CPU 使用率、开始时间、优先级等
This class requires Windows 2000 or higher.
此类需要 Windows 2000 或更高版本。