2

Is there a way how to read the System.Thread.ThreadState property of a managed thread by Profiling API ICofProfilerInfo or IMetadataImport interface or any other way from unmanaged code?

For example something like this:

ICorProfilerInfo3 pProfilerInfo = ...;
ThreadID threadId;
pProfilerInfo->GetCurrentThreadID(&threadId);
ThreadState threadState;
pSomethingWhatINeed->GetManagedTheadState(threadId, &threadState);
if(threadState == THREADSTATE_WAIT_JOIN_SLEEP){
  //do something
}
honzajscz
  • 2,850
  • 1
  • 27
  • 29

1 Answers1

3

You can use ICorDebug.GetProcess to get ICorDebugProcess, from which you can get ICorDebugThread by calling GetThread. Then call GetUserState to obtain CorDebugUserState. Beware, though, when using ICorDebug debugger and debugee have to be different processes.

MagnatLU
  • 5,967
  • 1
  • 22
  • 17
  • That is great idea and useful hint, however I need to get this information in a process that uses heavily ICorProfiler. – honzajscz Nov 26 '11 at 21:40
  • 1
    I guess my original answer wasn't clear enough - I have edited it. It's perfectly fine to use ICorDebug and ICorProfiler at the same time from one process on another process. My point was that process can't use ICorDebug to debug itself (unlike ICorProfiler - for which process can be its own profiler). – MagnatLU Nov 26 '11 at 21:57