gpt4 book ai didi

c# - 如何获取.Net Core中进程的CPU使用率和虚拟内存?

转载 作者:行者123 更新时间:2023-12-02 01:18:42 25 4
gpt4 key购买 nike

在.NET Core中,如何获取给定进程的CPU使用率和虚拟内存?

Google 搜索结果显示 PerformanceCounter 和 DriverInfo 类可以完成这项工作。但是,PerformanceCounter 和 DriverInfo 类在 .NET Core 中不可用。

stackoverflow上有一篇关于这个问题的帖子:How to get the current CPU/RAM/Disk usage in a C# web application using .NET CORE?

但是它只解决:-当前进程的CPU使用率:

    var proc = Process.GetCurrentProcess();

我已获得进程(具有 ProcessID 整数格式)。如何获取 .NET Core 中特定进程的 CPU 使用率和虚拟内存?

最佳答案

您可以在System.Diagnostics.PerformanceCounter package中使用PerformnceCounter

例如,下一个代码将为您提供总处理器使用百分比

var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
var value = cpuCounter.NextValue();
// In most cases you need to call .NextValue() twice
if (Math.Abs(value) <= 0.00)
value = cpuCounter.NextValue();

Console.WriteLine(value);

关于c# - 如何获取.Net Core中进程的CPU使用率和虚拟内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54178660/

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