3

What I actually search for is c++/win32 equivalent for .net ThreadState Enumeration.

Any suggestions?

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
honzajscz
  • 2,850
  • 1
  • 27
  • 29
  • NtQuerySystemInformation() but it isn't documented. Only documented way is WMI with the Win32_Thread class. – Hans Passant Nov 18 '11 at 16:35
  • Thank you all for you answers I really do appreciate your effort!!! However, I am looking for something like BOOL IsThreadInWaitJoinSleep(HANDLE threadHandle)? Any other suggestions? – honzajscz Nov 18 '11 at 23:32
  • 1
    You can't get that. Same kind of reason that you can't find out that a file is locked without actually trying to open it. Because such an operation is hopelessly unusable on a multitasking operating system. A nanosecond later the file might be locked. Or the thread might be blocked. – Hans Passant Nov 18 '11 at 23:42
  • @Hans Passant: How come then that I can get this information for a .net thread? – honzajscz Nov 19 '11 at 00:05
  • You can't, there's no way to map a ProcessThread to a Thread. Strictly informational only. – Hans Passant Nov 19 '11 at 00:32

4 Answers4

3

There's very little difference between these, all are waits for different kernel objects.

By "Wait" I assume you mean "I/O wait". "Join" is just "wait for a thread/process". And "Sleep" is "wait for a timer".

To complicate matters more, a thread can be waiting for some combination of kernel objects.

You could find out what objects a thread is waiting for, and the type of those objects, using a kernel debugger. I don't think there's any simpler way.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

There is no direct equivalent - managed and unmanaged threads should not be considered the same. See here.

An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the Fiber API to schedule many managed threads against the same operating system thread, or to move a managed thread among different operating system threads.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
1

The "problem" is that the .NET run time has ownership of your .NET thread. So when you call Abort for example, the run time internals throws a ThreadAbortException exception in the .NET context of your .NET thread, and that's how you can catch it in your thread using catch(ThreadAbortException).

And the same is true with ThreadState, since it has the underlying ownership of your thread, it knows exactly what it's doing and therefore can produce a valid thread state.

Since there is no non-hackins way to officially to query a thread for it's internal state, you could wrap this up in a class. But again, you would entirely depend upon the thread method to adhere to any .Abort()-requests.

CS.
  • 1,845
  • 1
  • 19
  • 38
1

The only state of a native thread easily accesible using the winapi is for knowning if the thread has finished its execution. Just use the function WaitForSingleObject() with the thread handle and a timeout of 0:

DWORD res = WaitForSingleObject(handleThread, 0);
switch (res)
{
    case WAIT_OBJECT_0:
        printf("The thread has finished\n");
        break;
    case WAIT_TIMEOUT:
        printf("The thread is still running\n");
        break;
    default:
        printf("Unknown error (shouldn't happen)\n");
        break;

}
rodrigo
  • 94,151
  • 12
  • 143
  • 190