0


I want to find out whether or not a certain process is sleeping or not (C++/Windows).
I'm trying to use the suspend count to do so and suspending to process before the check for
profiling processes.

I'm doing something like this:

SuspendThread(threadHandle);

... Do Some Stuff ...

int suspended = ResumeThread(threadHandle);
if (suspended > 1)
    m_isSleeping = true;

According to MSDN: http://msdn.microsoft.com/en-us/library/ms685086%28v=vs.85%29.aspx
If a process is suspended, "ResumeThread" returns a value > 0.
In my case, the process is a sleeping process, so I'd expect that the suspend count would be [My Call To SuspendThread] + [The "Sleep" method within the process] = 2
but I keep getting: ResumeThread(threadHandle) == 1

Does anybody know why it happens?
thanks :)

Idov
  • 5,006
  • 17
  • 69
  • 106
  • Do you want to know how to tell if a process is sleeping or do you want to know why the return value isn't what you expect? – i_am_jorf Jan 05 '11 at 18:23
  • I'd like to know how to tell if a process is sleeping. (I'd also like to know why I'm getting this result, but it's not the main issue :)) – Idov Jan 05 '11 at 18:26
  • 2
    You made an assumption that a sleeping thread is a suspended thread and that suspend count should increase when you put a thread to sleep. This is an incorrect assumption. That's all there is to it. – AnT stands with Russia Jan 05 '11 at 18:30
  • 1
    Although you might have *meant* to ask how to detect that a thread is sleeping, that's not what you really asked. You merely asked why you're getting an unexpected result from `ResumeThread`. To find out how to detect a sleeping thread, I suggest you post a new question. You've already gotten answers to this question, so it would be wrong to edit it to ask something else. – Rob Kennedy Jan 05 '11 at 18:38

3 Answers3

2

A thread in Sleep isn't suspended, hence the return value of 1

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
1

You're confusing threads and processes. ResumeThread and SuspendThread do not operate on process handles, they operate on thread handles. Also, Sleep does not change the suspend count of a process, only ResumeThread and SuspendThread change that. If you're trying to detect if a thread is currently in a Sleep call, you're doing it wrong.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • @Idov: You can't. See http://stackoverflow.com/questions/1006691/how-to-check-if-a-win32-thread-is-running-or-in-suspended-state . – Adam Rosenfield Jan 05 '11 at 18:43
  • @Idov: the question is *why* do you want to do this? – Yakov Galka Jan 05 '11 at 18:44
  • @Adam Rosenfield: Why? and what does your link has to do with it? @ybungalobill: http://stackoverflow.com/questions/4587065/measuring-time-of-a-profiled-sleep-function – Idov Jan 05 '11 at 18:58
  • 2
    @Idov: A thread can be in 3 states: Running, ready, or waiting. A running thread is one that is actually running on a CPU. A ready thread is one that is capable of running but has not yet been scheduled to run on a CPU. A waiting thread is a thread that cannot run because it is waiting for something: it's sleeping, it's blocked on I/O or a synchronization object, etc. The runnable states are running and ready. – Adam Rosenfield Jan 05 '11 at 19:36
  • OK! then if I get the thread's state and it's waiting, it may be sleeping? If so, it's good enough for me. It's even better :) – Idov Jan 05 '11 at 19:49
0

in addition to what others said, your SuspendThread call is not guaranteed to suspend thread immediately, it can be running for a time, and you can actually call ResumeThread while thread is still running (see details: http://www.dcl.hpi.uni-potsdam.de/research/WRK/2009/01/what-does-suspendthread-really-do/)

Gene Bushuyev
  • 5,512
  • 20
  • 19