How to get CPU Usage and Virtual Memory for a process in .Net Core?

前端 未结 2 853
借酒劲吻你
借酒劲吻你 2021-01-20 00:55

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

Google search result reveals that PerformanceCounter and DriverInfo class could do the

相关标签:
2条回答
  • 2021-01-20 01:41

    I just typed in 'get all processes in c#' into google and found this:

            Process[] processlist = Process.GetProcesses();
    
            foreach (Process theprocess in processlist)
            {
            }
    

    i quickly tested in asp.net core 2.2 and it works fine using System.Diagnostics; However i used windows 10 pro to test it. So i dont know if it will work with other operating systems.

    Source: https://www.howtogeek.com/howto/programming/get-a-list-of-running-processes-in-c/

    0 讨论(0)
  • 2021-01-20 01:43

    You can use PerformnceCounter in the System.Diagnostics.PerformanceCounter package

    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);
    
    0 讨论(0)
提交回复
热议问题