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

314 Views Asked by At

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
}
1

There are 1 best solutions below

2
MagnatLU On BEST ANSWER

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.