Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get CPU usage in NET Core

Shulou Source: shulou.com Published: 2022-06-02 03:56:25 10月16日 Update

This article mainly introduces "how to get CPU utilization in NET Core". In daily operation, I believe many people have doubts about how to obtain CPU utilization in NET Core. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to get CPU utilization in NET Core"! Next, please follow the editor to study!

In .NET Framework, a lot of people would use the PerformanceCounter class to do this

The code is as follows:

Public class Program {public static void Main (string [] args) {while (true) {var cpuUsage = GetCpuUsageForProcess (); Console.WriteLine (cpuUsage);}} private static int GetCpuUsageForProcess () {var currentProcessName = Process.GetCurrentProcess () .ProcessName Var cpuCounter = new PerformanceCounter ("Process", "% Processor Time", currentProcessName); cpuCounter.NextValue (); return (int) cpuCounter.NextValue ();}

However, PerformanceCounter does not exist in .NET Core, so we can only use other methods. In fact, there is a TotalProcessorTime property in the System.Diagnostics.Process class, which can count the CPU processor time consumed by the current process in quasi-real time.

The code is as follows:

Class Program {public static async Task Main (string [] args) {var task = Task.Run (() = > ConsumeCPU (50)); while (true) {await Task.Delay (2000); var cpuUsage = await GetCpuUsageForProcess (); Console.WriteLine (cpuUsage) }} public static void ConsumeCPU (int percentage) {Stopwatch watch = new Stopwatch (); watch.Start (); while (true) {if (watch.ElapsedMilliseconds > percentage) {Thread.Sleep (100-percentage); watch.Reset () Watch.Start ();} private static async Task GetCpuUsageForProcess () {var startTime = DateTime.UtcNow; var startCpuUsage = Process.GetCurrentProcess () .TotalProcessorTime; await Task.Delay; var endTime = DateTime.UtcNow; var endCpuUsage = Process.GetCurrentProcess () .TotalProcessorTime Var cpuUsedMs = (endCpuUsage-startCpuUsage) .TotalMilliseconds; var totalMsPassed = (endTime-startTime) .TotalMilliseconds; var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed); return cpuUsageTotal * 100;}}

You can see that the program outputs every 2 seconds, and it is observed that the CPU utilization in output and task manager is basically the same.

At this point, the study on "how to get CPU utilization in NET Core" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Usage learning code more help practical consistent next things tasks utilization processors real-time attributes articles ways methods time theory knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi OPPO Reno Shulou Tech Info Linux