鉴于以下代码,我可以得到给定进程的活动线程总数:
using (var p = Process.GetCurrentProcess())
{
Console.WriteLine(p.Threads.Count);
}
我想使用PerformanceCounters
实现这一目标,并且到目前为止已尝试过(参考:https://msdn.microsoft.com/en-us/library/w8f5kw2e(v=vs.110).aspx):
var category = ".NET CLR LocksAndThreads";
var counterOne = "# of current logical Threads";
var counterTwo = "# of current physical Threads";
var coutnerThree = "# of current recognized threads";
var counterFour = "# of total recognized Threads";
var perfCounter = new PerformanceCounter(
category,
counterOne,
"myProcess") { ReadOnly = true };
Console.WriteLine("Thread count: ", perfCounter.NextValue());
我无法匹配Process
返回的号码。非常感谢任何帮助。
答案 0 :(得分:3)
Process.Threads
将包括所有线程,包括本机OS线程。 .NET性能计数器只会返回托管线程。匹配Process.Threads
的效果计数器为\\Process(myProcess)\Thread Count