C# 性能监视器始终显示100%
在观看关于如何制作性能监视器的视频时,我遇到了一个bug。它只会显示100%的CPU负载,即使我的其他监控应用程序都说它只有5%的空闲 这是密码C# 性能监视器始终显示100%,c#,C#,在观看关于如何制作性能监视器的视频时,我遇到了一个bug。它只会显示100%的CPU负载,即使我的其他监控应用程序都说它只有5%的空闲 这是密码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using Syste
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
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统诊断;
使用系统线程;
使用系统、语音、合成;
命名空间CPU
{
班级计划
{
专用静态语音合成器synth=新语音合成器();
静态void Main(字符串[]参数)
{
//Cpu消息
List cpumaxedOutMessages=new List();
添加(“警告CPU满载”);
添加(“CPU以100%的速度运行”);
添加(“CPU在循环”);
添加(“苏联导弹发射,等等。不是这样的,我只是因为一直处于100%的温度而过热!”);
cpumaxedOutMessages.Add(“达到DEF CON 1”);
Random rand=新的Random();
//欢迎用户
Console.ForegroundColor=ConsoleColor.Pigenta;
Console.WriteLine(“性能监视器”);
synth.Speak(“欢迎来到性能监视器”);
#区域我的性能监视器
//这将CPU Cpde拉入%
PerformanceCounter perfCpuCount=新的PerformanceCounter(“处理器信息”,“处理器时间百分比”,“总计”);
perfCpuCount.NextValue();
//内存计数
PerformanceCounter performCount=新的PerformanceCounter(“内存”、“可用MB”);
perfMemCount.NextValue();
//启动时间(秒)
PerformanceCounter perfUptimeCount=新的PerformanceCounter(“系统”、“系统启动时间”);
perfUptimeCount.NextValue();
TimeSpan uptimeSpan=TimeSpan.FromSeconds(perfUptimeCount.NextValue());
string systemUpTimeMessage=string.Format(“当前系统启动时间为{0}天{1}小时{2}分钟{3}秒”,
(int)uptimeSpan.TotalDays,
(int)uptimeSpan.Hours,
(int)uptimeSpan.Minutes,
(int)uptimeSpan.Seconds
);
int speechSpeed=1;
//告诉用户正常运行时间是多少
说话(系统上传信息,语音性别。女性,3);
//无限循环
while(true)
{
Console.Clear();
//每1秒打印一次CPU负载
//获取当前的性能值
int currentcupercentage=(int)perfCpuCount.NextValue();
int currentAvailableMemory=(int)perfMemCount.NextValue();
Console.ForegroundColor=ConsoleColor.Yellow;
WriteLine(“CPU负载:{0}%”,currentCpuPercentage);
Console.ForegroundColor=ConsoleColor.Cyan;
WriteLine(“可用内存:{0}MB”,currentAvailableMemory);
如果(当前CPupercentage>80)
{
如果(currentCpuPercentage==100)
{
字符串cpuLoadVocalMessage=cpumaxedOutMessages[rand.Next(5)];
说话(cpuLoadVocalMessage,VoiceGender.Male,speechSpeed);
}
其他的
{
string cpuLoadVocalMessage=string.Format(“当前Cpu负载为{0}%,currentCpuPercentage”);
说话(cpuLoadVocalMessage,语音性别。女性,3);
}
//记忆
如果(当前可用内存>1024)
{
string memAvailableVocalMessage=string.Format(“您当前有{0}兆字节的可用内存”,currentAvailableMemory);
说话(memAvailableVocalMessage,语音性别,男性,5岁);
}
其他的
睡眠(1000);
}//循环结束
}
}
//用精选的声音说话
公共静态void Speak(字符串消息、VoiceGender VoiceGender)
{
合成。选择VoiceByHits(语音性别);
synth.Speak(消息);
}
//用选择的声音和功能说话
公共静态void Speak(字符串消息、VoiceGender VoiceGender、整数速率)
{
合成速率=速率;
说话(信息、声音、性别);
}
}
}
#端区
附近某处丢失了一些东西:
else
Thread.Sleep(1000);
结果是代码很少(如果有的话)等待1秒。当
currentAvailableMemory时,您的循环将导致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
}
非常感谢,这就解决了它。