性能监视器始终显示100%
本文关键字:显示 100% 性能监视 监视器 性能 | 更新日期: 2023-09-27 18:19:33
在观看关于如何制作性能监视器的视频时,我遇到了一个错误。它只显示100%的CPU负载,即使我使用的其他监控应用程序都说它只有5%的空闲。
这是代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System.Speech.Synthesis;
namespace CPU
{
class Program
{
private static SpeechSynthesizer synth = new SpeechSynthesizer();
static void Main(string[] args)
{
//Cpu messages
List<string> cpumaxedOutMessages = new List<string>();
cpumaxedOutMessages.Add("Warning CPU FULL LOAD");
cpumaxedOutMessages.Add("CPU RUNNING AT 100% SLOW DOWN");
cpumaxedOutMessages.Add("CPU IS GOING AROUND IN CIRCLES");
cpumaxedOutMessages.Add("SOVIET MISSILE LAUNCH, WAIT. ITS NOT, I JUST AM OVERHEATING FROM BEING AT 100% ALL THE TIME!");
cpumaxedOutMessages.Add("DEF CON 1 REACHED");
Random rand = new Random();
//Greets user
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Performance Monitor");
synth.Speak("Welcome to the Performance Monitor");
# region My PerfMonitor
//This pulls CPU Cpde in %
PerformanceCounter perfCpuCount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
perfCpuCount.NextValue();
//Mem count
PerformanceCounter perfMemCount = new PerformanceCounter("Memory", "Available MBytes");
perfMemCount.NextValue();
//Up time (In Sec)
PerformanceCounter perfUptimeCount = new PerformanceCounter("System", "System Up Time");
perfUptimeCount.NextValue();
TimeSpan uptimeSpan = TimeSpan.FromSeconds(perfUptimeCount.NextValue());
string systemUpTimeMessage = string.Format("The current System up time is {0} days {1} Hours {2} Minutes {3} Seconds",
(int)uptimeSpan.TotalDays,
(int)uptimeSpan.Hours,
(int)uptimeSpan.Minutes,
(int)uptimeSpan.Seconds
);
int speechSpeed = 1;
//Tell user what uptime is
Speak(systemUpTimeMessage, VoiceGender.Female, 3);
//Infinite Loop
while (true)
{
Console.Clear();
//Every 1 Second Prints CPU Load
//Gets current Perf Values
int currentCpuPercentage = (int)perfCpuCount.NextValue();
int currentAvailableMemory = (int)perfMemCount.NextValue();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("CPU Load : {0}%", currentCpuPercentage);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Available Memory: {0}MB", currentAvailableMemory);
if (currentCpuPercentage > 80)
{
if (currentCpuPercentage == 100)
{
string cpuLoadVocalMessage = cpumaxedOutMessages[rand.Next(5)];
Speak(cpuLoadVocalMessage, VoiceGender.Male, speechSpeed);
}
else
{
string cpuLoadVocalMessage = String.Format("The current Cpu Load is {0} Percent", currentCpuPercentage);
Speak(cpuLoadVocalMessage, VoiceGender.Female, 3);
}
//Memory
if (currentAvailableMemory > 1024)
{
string memAvailableVocalMessage = String.Format("You currently have {0} Megabytes of memory available", currentAvailableMemory);
Speak(memAvailableVocalMessage, VoiceGender.Male ,5 );
}
else
Thread.Sleep(1000);
} //End of loop
}
}
//speaks with a selcted Voice
public static void Speak(string message, VoiceGender voiceGender)
{
synth.SelectVoiceByHints(voiceGender);
synth.Speak(message);
}
//speaks with selcted voice and function
public static void Speak(string message, VoiceGender voiceGender, int rate)
{
synth.Rate = rate;
Speak(message, voiceGender);
}
}
}
#endregion
您的循环导致了100%的处理负载。应该是这样的:
while (true)
{
Console.Clear();
//Every 1 Second Prints CPU Load
//Gets current Perf Values
int currentCpuPercentage = (int)perfCpuCount.NextValue();
int currentAvailableMemory = (int)perfMemCount.NextValue();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("CPU Load : {0}%", currentCpuPercentage);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Available Memory: {0}MB", currentAvailableMemory);
if (currentCpuPercentage > 80)
{
if (currentCpuPercentage == 100)
{
string cpuLoadVocalMessage = cpumaxedOutMessages[rand.Next(5)];
Speak(cpuLoadVocalMessage, VoiceGender.Male, speechSpeed);
}
else
{
string cpuLoadVocalMessage = String.Format("The current Cpu Load is {0} Percent", currentCpuPercentage);
Speak(cpuLoadVocalMessage, VoiceGender.Female, 3);
}
//Memory
if (currentAvailableMemory > 1024)
{
string memAvailableVocalMessage = String.Format("You currently have {0} Megabytes of memory available", currentAvailableMemory);
Speak(memAvailableVocalMessage, VoiceGender.Male, 5);
}
} //End of loop
Thread.Sleep(1000); //note that the sleep now gets called every update cycle
}
附近有东西丢失了:
else
Thread.Sleep(1000);
结果是,代码很少(如果有的话)等待1秒。当currentAvailableMemory <= 1024
时,它将等待。