Get thread state, Suspended or running

Is there any way to get a threads state from the ThreadID or Thread handle.

I need a way to check if a thread is suspended.
LVL 2
heretoreadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CoderAlexCommented:
 You can use:
 
  GetThreadTimes

The GetThreadTimes function retrieves timing information for the specified thread.

funciton GetThreadTimes(hThread: THANDLE, // handle to thread
                                     lpCreationTime: LPFILETIME, // thread creation time
                                     lpExitTime: LPFILETIME, // thread exit time
                                     lpKernelTime: LPFILETIME, // thread kernel-mode time
                                     lpUserTime: LPFILETIME // thread user-mode time): BOOL;


  You can also use GetExitCodeThread to verify if the thread is finished or not.
heretoreadAuthor Commented:
but if a process was created suspened, would the main thread have 0 user and kernel mode time?
Wim ten BrinkSelf-employed developerCommented:
Call SuspendThread followed by a ResumeThread for the ThreadID. The SuspendThread will return the number of times that the thread has been suspended. It returns 0 if it was running. That's actually the fun thing about threads. They have a counter and as long as this counter is greater to 0, the thread is suspended. SuspendThread increases the counter while ResumeThread decreases it again.

Don't forget to resume immediately after the suspend and do realise that this might make your thread miss a clockcycle...
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

heretoreadAuthor Commented:
I did consider that, but didn't want to effect execution of thread. Thats why i'm looking for a query way.
Wim ten BrinkSelf-employed developerCommented:
Well, a thread has a counter thus there might be a way to access the counter without effecting the thread execution. Especially since my solution might be troublesome on systems with multiple processors or with hyperthreading because then you might have two threads running at the same time.

I have been reading a bit in a book called "undocumented Windows 2000 secrets" and it mentions a C++ structure cakked _KTHREAD which is apparantly a structure full information about a thread. It has a byte field for the thread state, which would probably be useful to you. However, this solution requires good knowledge of the Windows kernel and the use of undocumented Windows API functions. (And those might not be supported by the next Windows version.)
heretoreadAuthor Commented:
I'm now using NtQuerySystemInformation to get the process list, this also allows me to get a record on each thread with the info i need.
CetusMODCommented:
PAQed with points refunded (120)

CetusMOD
Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.