How to get CPU utilization with C # real-time

xiaoxiao2021-03-06  129

Author: Meng will come from: [it] would be a wonderful world Meng Date: 2003 at 8:01:35 on June 18

using System; using System.Diagnostics; using System.Threading; public class CpuLoadInfo {// auxiliary print methods private static void Say (string txt) {Console.WriteLine (txt);} // auxiliary print methods private static void Say () {SAY ("");} // the main method. Command line arguments area ignored. [Stathread] public static void main () {SAY ("$ ID: cpuloadInfo.cs, v 1.2 2002/08/17 17:45 : 48 RZ65 EXP $ "); Say (); Say (" Attempt to Create a PerformanceCounter Instance: "); SAY (" Category Name = " CategoryName); Say (" Counter Name = " countername); SAY Instance name = " InstanceName); PerformanceCounter pc = new PerformanceCounter (CategoryName, CounterName, InstanceName); Say (" Performance counter was created "); Say (" Property CounterType:. " pc.CounterType); Say (); Say ("Property Counterhelp: pc.counterhelp); Say (); Say (" Entering Measurement Loop. "); While (TRUE) {thread.sleep (1000); // Wait for 1 Second Float cpmbuload = pc.nextValue (); SAY ("CPU LOAD =" CPULOAD "%.");}} // constants used to select the performance counter private const string CategoryName = "Processor"; private const string CounterName = "% Processor Time"; private const string InstanceName = "_Total";} This is a calculation result on my computer:. Entering measurement Loop. CPU LOAD = 0%. CPU LOAD = 1.941746%. CPU LOAD = 4.99997%. CPU LOAD = 0.99997%. CPU LOAD = 0.9900987%. CPU LOAD = 0%.

转载请注明原文地址:https://www.9cbs.com/read-127030.html

New Post(0)