本文介绍了如何在 .Net Core 中获取进程的 CPU 使用率和虚拟内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In .NET Core, how to get the CPU usage and Virtual Memory for a given process?

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

Google search result reveals that PerformanceCounter and DriverInfo class could do the job. However, PerformanceCounter & DriverInfo class are not available in .NET Core.

stackoverflow 中有一篇关于这个问题的帖子:如何在使用 .NET CORE 的 C# Web 应用程序中获取当前 CPU/RAM/磁盘使用情况?

There is a post in stackoverflow about this question: How to get the current CPU/RAM/Disk usage in a C# web application using .NET CORE?

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

However it only addresses:-CPU usage for the current process:

    var proc = Process.GetCurrentProcess();

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

I have been given process (with a ProcessID integer format). How do I get the CPU Usage and Virtual Memory for that particular process in .NET Core?

推荐答案

您可以在 System.Diagnostics.PerformanceCounter

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

for example, the next code will give you the total processor usage percent

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

这篇关于如何在 .Net Core 中获取进程的 CPU 使用率和虚拟内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:43