#author("2020-01-25T15:02:00+09:00","","") #author("2020-02-01T17:12:29+09:00","","") #navi(../) * CPU''コア毎''の使用率を取得するサンプルコード [#cfaa3b2b] .NETを使ったCPU''コア毎''使用率を取得するサンプルコードになります。~ CPU全体の使用率を取得したい場合は、以下のリンク記事を参照ください。~ >[[CPU使用率を取得するサンプルコード>.NET/CPU使用率を取得する]]~ サンプルコードは C#, Visual Basic(VB)を公開しています。 #htmlinsert(windev-top.html) #contents * 関連サイト [#wcf28306] -[[Microsoft | .NET PerformanceCounter クラス>https://docs.microsoft.com/ja-jp/dotnet/api/system.diagnostics.performancecounter]] -[[Microsoft | .NET Environment クラス>https://docs.microsoft.com/ja-jp/dotnet/api/system.environment]] * 関連記事 [#e9086bfe] -[[CPU使用率を取得するサンプルコード>.NET/CPU使用率を取得する]] -[[WMIを使って物理メモリ・仮想メモリの使用量等を取得する>.NET/WMIを使って物理メモリ・仮想メモリの使用量等を取得する]] * 動作確認環境 [#c78f2983] -Windows 10 ver.1909 -Visual Studio 2019 -.NET Framework 4.7.2 -コンソールアプリケーション * CPU''コア毎''の使用率取得のサンプルコード [#y5500d0a] 以下に C#, Visual Basic(VB) によるCPU使用率を取得するサンプルコードを記します。~ 約1秒ごとにCPU使用率を表示します。~ CPUコア数は、Environment.ProcessorCount 値をコア数として使っています。 ** C# によるCPU使用率取得サンプルコード [#g5aa6a99] using System; using System.Diagnostics; using System.Threading; class CpuCoreUsage { private PerformanceCounter[] cpuCounters; static void Main(string[] args) { CpuCoreUsage cpuCoreUsage = new CpuCoreUsage { cpuCounters = new PerformanceCounter[Environment.ProcessorCount] }; for (int i = 0; i < cpuCoreUsage.cpuCounters.Length; i++) { cpuCoreUsage.cpuCounters[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString()); } string header= ""; for (int i = 0; i < cpuCoreUsage.cpuCounters.Length; i++) { if (!string.IsNullOrEmpty(header)) { header += " ,"; } header += "CPU" + i.ToString(); } Console.WriteLine(header); while (true) { float[] coreUsageValues = cpuCoreUsage.GetCpuCoreUsage(cpuCoreUsage.cpuCounters); string result = ""; for (int i = 0; i < coreUsageValues.Length;i++) { if (!string.IsNullOrEmpty(result)) { result += " ,"; } result += coreUsageValues[i]; } Console.WriteLine(result); Thread.Sleep(1000); } } private float[] GetCpuCoreUsage(PerformanceCounter[] pcs) { float[] usageValues = new float[pcs.Length]; for(int i = 0; i < pcs.Length; i++) { usageValues[i] = pcs[i].NextValue(); } return usageValues; } } ** Visual Basic(VB) によるCPU使用率取得サンプルコード [#aeb813b4] Imports System.Threading Module Module1 Sub Main() Dim cpuCounters As PerformanceCounter() cpuCounters = New PerformanceCounter(Environment.ProcessorCount - 1) {} For i As Integer = 0 To cpuCounters.Length - 1 cpuCounters(i) = New PerformanceCounter("Processor", "% Processor Time", i.ToString()) Next Dim header As String = "" For i As Integer = 0 To cpuCounters.Length - 1 If Not String.IsNullOrEmpty(header) Then header += " ," End If header += "CPU" & i.ToString() Next Console.WriteLine(header) While True Dim coreUsageValues As Single() = GetCpuCoreUsage(cpuCounters) Dim result As String = "" For i As Integer = 0 To coreUsageValues.Length - 1 If Not String.IsNullOrEmpty(result) Then result += " ," End If result += coreUsageValues(i).ToString Next Console.WriteLine(result) Thread.Sleep(1000) End While End Sub Private Function GetCpuCoreUsage(pcs As PerformanceCounter()) As Single() Dim usageValues As Single() = New Single(pcs.Length - 1) {} For i As Integer = 0 To pcs.Length - 1 usageValues(i) = pcs(i).NextValue() Next Return usageValues End Function End Module ** 実行結果 [#ue07b54e] 上記のサンプルコードを実行したときのキャプチャです。 #ref(01.png) 以上、.NETでCPU使用率を取得するサンプルコードの紹介でした。 #htmlinsert(windev-btm.html)