如何獲得C#中的CPU使用量?我希望得到C#中應用程序的總CPU使用量。我已經找到了很多方法來深入了解進程的屬性,但是我只想要進程的CPU使用情況,以及像TaskManager中那樣的CPU總量。
3 回答

明月笑刀無情
TA貢獻1828條經驗 獲得超4個贊
public class Form1{ int totalHits = 0; public object getCPUCounter() { PerformanceCounter cpuCounter = new PerformanceCounter(); cpuCounter.CategoryName = "Processor"; cpuCounter.CounterName = "% Processor Time"; cpuCounter.InstanceName = "_Total"; // will always start at 0 dynamic firstValue = cpuCounter.NextValue(); System.Threading.Thread.Sleep(1000); // now matches task manager reading dynamic secondValue = cpuCounter.NextValue(); return secondValue; } private void Timer1_Tick(Object sender, EventArgs e) { int cpuPercent = getCPUCounter(); if (cpuPercent >= 90) { totalHits = totalHits + 1; if (totalHits == 60) { Interaction.MsgBox("ALERT 90% usage for 1 minute"); totalHits = 0; } } else { totalHits = 0; } Label1.Text = cpuPercent + " % CPU"; Label2.Text = getRAMCounter() + " RAM Free"; Label3.Text = totalHits + " seconds over 20% usage"; }}
- 3 回答
- 0 關注
- 853 瀏覽
添加回答
舉報
0/150
提交
取消