如何判断一个线程是挂起还是运行状态?

pgmsoul 2010-08-08 09:04:22
一个运行的线程,如何判断它的状态?
...全文
2562 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
pgmsoul 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

时间证明,8# 正解
[/Quote]

老早的问题了,现在看了一下,这个状态意义不大,本意是根据状态执行何种操作,但是那个状态既然是在另一个线程里获取,那么它是时刻变化的,即使得到一个正确的状态,执行下一步操作仍然不能保证有效,比如Resume了一个正在运行的线程。
sea310 2012-06-12
  • 打赏
  • 举报
回复
时间证明,8# 正解
binbinyantai 2012-02-15
  • 打赏
  • 举报
回复
根据 SuspendThread()和resumethread()函数的返回值来判断。如果挂起多次,就要resume多次。
吹雪 2012-02-15
  • 打赏
  • 举报
回复
ResumeThread

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.
pgmsoul 2010-08-23
  • 打赏
  • 举报
回复
无解,ms既然不提供这样的函数,侧面说明,很难操作。

#4可能可行,但是实在是很绕。
Jekhn 2010-08-08
  • 打赏
  • 举报
回复
我觉得这个状态应该在线程挂起和重新开始的时候去捉捕这个状态的,类似如下,不知道行不行,
bool FSuspended;
int SuspendCount = ResumeThread(FThreadHandle);
if (SuspendCount==1) {
FSuspended = false;
} else {
FSuspended = true;
}
schlafenhamster 2010-08-08
  • 打赏
  • 举报
回复
没有终止,才能知道是否挂起了。
ColdMooon 2010-08-08
  • 打赏
  • 举报
回复
3楼,不要误导新人行不。
STILL_ACTIVE只是说没有终止,没说是不是挂起了。
jwpl190 2010-08-08
  • 打赏
  • 举报
回复
NtQuerySystemInformation 第一个参数是5.
这个函数没有定义。你得手工得一下。
NtQuerySystemInformation = (PQUERYSYSTEM) GetProcAddress(
LoadLibrary( "ntdll.dll" ),
"NtQuerySystemInformation" );
在网上可以找找例子。
schlafenhamster 2010-08-08
  • 打赏
  • 举报
回复
BOOL GetExitCodeThread(
HANDLE hThread, // handle to the thread
LPDWORD lpExitCode // address to receive termination status
);
//
If the specified thread has not terminated, the termination status returned is STILL_ACTIVE.
pgmsoul 2010-08-08
  • 打赏
  • 举报
回复
假设循环如下:

while(1)
{
ThreadSuspend = 1;
SuspendThread(Handle);
ThreadSuspend = 0;
}

ThreadSuspend是标志线程状态的一个变量,问题是执行了,ThreadSuspend = 1后如果线程切换到了使用这个变量的线程,那么会得出线程已经挂起的结论,实际上线程还未挂起。
ColdMooon 2010-08-08
  • 打赏
  • 举报
回复
SuspendThread,返回值就是先前的挂起计数。
完了你再ResumeThread恢复就行了。

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧