楼上的方法忒麻烦,其实 .NET 已经把基础结构封装好了。
我们只要稍微变通一下就可以了。
下面是示例代码:
using System;
using System.Diagnostics;
namespace Lucifer.CSharp.Sample
{
class Program
{
static void Main()
{
PrintThreads(Process.GetCurrentProcess().Id);
}
private static void PrintThreads(int processId)
{
Process process = Process.GetProcessById(processId);
ProcessThreadCollection threads = process.Threads;
foreach (ProcessThread thread in threads)
{
Console.WriteLine("Thread Id : 0x{0}", thread.Id.ToString("X"));
}
}
}
}