0

I am trying to print info about all threads running in a given program/process, by using the code below. For a console program (VS2015, NET 4.6, Windows 10), I am getting five threads. The problem is that that class ProcessThread does not include many thread properties like about background, name, etc. The class Thread has more info but it is available only from the current thread and not from another thread, even of the same process. At the minimum I want to get info like IsBackground, and name/desc about the function of the thread.

 ProcessThreadCollection currentThreads = Process.GetCurrentProcess().Threads;
 int count = 0;
 Console.WriteLine("ManagedThreadId: {0}", Thread.CurrentThread.ManagedThreadId);
 Console.WriteLine("GetCurrentThreadId: {0}", AppDomain.GetCurrentThreadId());
 Console.WriteLine("Process name: {0}", Process.GetCurrentProcess().ProcessName);
 Console.WriteLine("Process threads: {0}", currentThreads.Count);
 foreach (ProcessThread thread in currentThreads)
 {
   Console.WriteLine(" Thread {0}:", ++count);
   Console.WriteLine("  Name: {0}", thread.ToString()); // meaningless
   Console.WriteLine("  GetType: {0}", thread.GetType()); // meaningless
   Console.WriteLine("  Id: {0}", thread.Id);
   Console.WriteLine("  State: {0}", thread.ThreadState);
   if (thread.ThreadState == System.Diagnostics.ThreadState.Wait)
       Console.WriteLine("  WaitReason: {0}", thread.WaitReason);
   Console.WriteLine("  PriorityLevel: {0}", thread.PriorityLevel);
   Console.WriteLine("  BasePriority: {0}", thread.BasePriority);
   Console.WriteLine("  CurrentPriority: {0}", thread.CurrentPriority);
 }
Micky6789
  • 43
  • 5
  • Because.. `ProcessThread != Thread` those are different types. A `ProcessThread` represents a win32 thread. `Thread` is a wrapper arround it. win32 thread doesn't contains a name for example. For more info: [Getting from ProcessThread to a managed thread](https://stackoverflow.com/questions/1749541/getting-from-processthread-to-a-managed-thread) – Jeroen van Langen May 23 '17 at 14:43
  • https://stackoverflow.com/questions/1749541/getting-from-processthread-to-a-managed-thread – René Vogt May 23 '17 at 14:46
  • https://github.com/Microsoft/clrmd – Hans Passant May 23 '17 at 14:47

0 Answers0