如何获取有关非当前线程的更多信息,如IsBackground?

时间:2017-05-23 14:41:02

标签: c#

我正在尝试打印有关在给定程序/进程中运行的所有线程的信息, 使用下面的代码。对于控制台程序(VS2015,NET 4.6,Windows 10), 我得到五个帖子。问题是那个类ProcessThread 不包括许多线程属性,如背景,名称等。 Thread类有更多信息,但只能从当前线程获得 而不是来自另一个线程,即使是同一个过程。至少我想得到像IsBackground这样的信息,并命名/ desc关于线程的功能。

 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);
 }

0 个答案:

没有答案