简体   繁体   English

如何知道线程处于挂起状态

[英]How to know a thread is in suspended state

I am suspending a thread using an event. 我正在使用事件挂起线程。 When the eventSuspend is signaled the thread waits on it. 通知eventSuspend时,线程在等待它。

// inside the thread function
WaitForSingleObject(eventSuspend, INFINITE );

Now from outside I set it to wait by eventSuspend.ResetEvent() but loop inside the thread function is kind of long (time consuming). 现在从外部将其设置为通过eventSuspend.ResetEvent()等待,但是线程函数内部的循环有点长(很耗时)。 How can I know that the thread has finished whatever it was doing and now it is indeed waiting on this signal? 我怎么知道线程已经完成了它正在做的任何事情,现在它确实在等待这个信号?

Your question is wrong: A thread that is waiting is not suspended! 您的问题是错误的:没有暂停正在等待的线程!

A thread is suspended after calling SuspendThread() but not after calling WaitForSingleObject() . 在调用SuspendThread()之后,线程将被挂起,但在调用WaitForSingleObject()之后,线程不会被挂起。

Simply set a BOOL flag when the thread loop starts and reset the flag when the thread loop exits then you know if it runs or not. 只需在线程循环开始时设置BOOL标志,并在线程循环退出时重置标志即可知道它是否在运行。

If you want futher informations about a thread, like if it is supended or not, you can use the code that I posted here: How to get thread state (eg suspended), memory + CPU usage, start time, priority, etc 如果您想了解有关线程的更多信息(例如是否被暂停),可以使用我在此处发布的代码: 如何获取线程状态(例如,挂起),内存+ CPU使用率,启动时间,优先级等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM