首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >性能监视器总是100%显示

性能监视器总是100%显示
EN

Stack Overflow用户
提问于 2014-08-26 02:20:08
回答 2查看 425关注 0票数 0

在跟踪一段关于如何制作性能监视器的视频时,我遇到了一个bug。它只显示100%的CPU负载,即使我说过的所有其他监控应用程序都是在5%的空闲状态下。

这是代码

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-26 02:29:46

您的循环导致了100%的处理负载。应该是这样的:

代码语言:javascript
复制
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
            }
票数 2
EN

Stack Overflow用户

发布于 2014-08-26 02:26:09

有些东西在附近丢了:

代码语言:javascript
复制
 else

 Thread.Sleep(1000);

其结果是代码很少等待1秒(如果有的话)。当currentAvailableMemory <= 1024时,它会等待。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25497066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档