Search code examples
c#.netprofilingthread-stateimetadataimport

How to read System.Threading.Thread.ThreadState by Profiling API (ICofProfilerInfo or any IMetadataImport or any otherway)


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
}

Solution

  • 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.