gpt4 book ai didi

c# - 如何使用性能计数器获取进程的运行线程数?

转载 作者:行者123 更新时间:2023-11-30 21:48:15 27 4
gpt4 key购买 nike

给出下面的代码,我可以获得给定进程的事件线程总数:

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 返回的数字。非常感谢任何帮助。

最佳答案

Process.Threads 将包括所有线程,包括 native 操作系统线程。 .NET 性能计数器只会返回托管线程。匹配 Process.Threads 的性能计数器将是 \\Process(myProcess)\Thread Count

关于c# - 如何使用性能计数器获取进程的运行线程数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37793023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com