注意:算法仅供参考。
cpuusage.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.IO; 5 using System.Linq; 6 using System.Text; 7 using System.Threading; 8 9 namespace cpuusage 10 { 11 class Program 12 { 13 static readonly Dictionary<string, PerformanceCounter> _caches = new Dictionary<string, PerformanceCounter>(); 14 15 static void Main(string[] args) 16 { 17 const string nameTitle = "Name"; 18 const string cpuUsageTitle = "CPU Usage (%)"; 19 var processNames = GetProcessNames(args); 20 var nameColumnMaxLength = Math.Max(processNames.Max(n => n.Length), nameTitle.Length); 21 var cpuUsageColumnMaxLength = cpuUsageTitle.Length; 22 var format = string.Format("{{0,-{0}}} {{1:0.##}}", nameColumnMaxLength); 23 var head = string.Format(format, nameTitle, cpuUsageTitle).ToUpper(); 24 head += Environment.NewLine + string.Format(format, new string('-', nameColumnMaxLength), new string('-', cpuUsageColumnMaxLength)); 25 while (true) 26 { 27 Console.WriteLine(head); 28 foreach (var name in processNames) 29 { 30 try 31 { 32 Console.WriteLine(format, name, GetProcessCpuUsage(name)); 33 } 34 catch(Exception) 35 { 36 } 37 } 38 Thread.Sleep(1000); 39 Console.WriteLine(); 40 } 41 42 } 43 44 static string[] GetProcessNames(string[] args) 45 { 46 if (args.Length == 0) 47 { 48 var n = Path.GetFileName(Environment.GetCommandLineArgs()[0]); 49 var n1 = Path.GetFileNameWithoutExtension(n); 50 Console.WriteLine("Example:\n {0} _Total IDLE System Svchost {1}\n {0} *\n", n, n1); 51 return new string[] { "_Total", "IDLE", "system", "Svchost", n1 }; 52 } 53 54 if (args[0] == "*") 55 { 56 return Process.GetProcesses().Select(n => n.ProcessName).OrderBy(n => n).Distinct().ToArray(); 57 } 58 59 return args; 60 } 61 62 static float GetProcessCpuUsage(string instanceName) 63 { 64 // var total = GetPerformanceCounter("_Total").NextValue(); 65 var value = GetPerformanceCounter(instanceName).NextValue(); 66 return value / Environment.ProcessorCount; 67 } 68 69 static PerformanceCounter GetPerformanceCounter(string instanceName) 70 { 71 PerformanceCounter r; 72 if (_caches.TryGetValue(instanceName, out r)) 73 { 74 return r; 75 } 76 r = new PerformanceCounter("Process", "% Processor Time", instanceName); 77 _caches[instanceName] = r; 78 return r; 79 } 80 81 } 82 }
build.bat
1 @echo off 2 pushd "%~dp0" 3 set csfile=cpuusage.cs 4 set PATH="C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\bin\Roslyn";"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\Roslyn";"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\Roslyn";"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64";"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN";C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319;C:\WINDOWS\Microsoft.NET\Framework64\v3.5;C:\WINDOWS\Microsoft.NET\Framework64\v2.0;%PATH% 5 csc.exe /target:exe /unsafe+ %csfile% 6 if ERRORLEVEL 1 (echo ERROR: %ERRORLEVEL%) else (echo Build Success) 7 echo. 8 echo Press any key to EXIT... 9 pause>nul 10 popd
demo