如何在c# . net中使用温度性能监视器

本文关键字:温度 性能 性能监视 监视器 net | 更新日期: 2023-09-27 18:06:43

我试图为盲人和视障人士创建一个使用语音的应用程序,告诉你系统的正常运行时间,你有多少RAM可用,当前的CPU负载,以及其他类似的东西,我遇到了一个问题;我无法使温度性能计数器正常工作

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 ConsoleApplication2
{
    class Program
    {
        private static SpeechSynthesizer synth = new SpeechSynthesizer();
        static void Main(string[] args)
        {
            List<string> cpuMaxedOutMessage = new List<string>();
            cpuMaxedOutMessage.Add("WARNING: Reduce the load on your CPU!");
            cpuMaxedOutMessage.Add("Don't push your CPU so hard! Your CPU usage is at 100%");
            cpuMaxedOutMessage.Add("CPU OVERLOAD!");
            cpuMaxedOutMessage.Add("Congratulations. You have officially maxed your CPU.");
            cpuMaxedOutMessage.Add("WARNING: Reduce the load on your CPU!");
            Random rand = new Random();
            synth.Speak("Welcome to System Resource Monitor: Vocal Edition Version 1.0");
            PerformanceCounter perfCpuCount = new PerformanceCounter("Processor Information","% Processor Time","_Total");
            perfCpuCount.NextValue();
            PerformanceCounter perfMemCount = new PerformanceCounter("Memory", "Available Mbytes");
            perfMemCount.NextValue();
            PerformanceCounter perfUptimeCount = new PerformanceCounter("System", "System Up Time");
            perfUptimeCount.NextValue();
            PerformanceCounter perfTempZone = new PerformanceCounter("Thermal Zone Information", "Temperature", "'_TZ.TZ00");
            perfTempZone.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);
            JoshSpeak(systemUptimeMessage, VoiceGender.Male, 2);
            int speechSpeed = 1;
            while (true)
            {
                int currentCpuPercentage = (int)perfCpuCount.NextValue();
                int currentAvailableMemory = (int)perfMemCount.NextValue();
                int currentTemp = (int)perfTempZone.NextValue();

                Console.WriteLine("CPU load: {0}%", currentCpuPercentage);
                Console.WriteLine("Available Memory: {0}MB", currentAvailableMemory);
                Console.WriteLine("Your Current Temperature is: {0} degrees", currentTemp);
                    if (currentCpuPercentage == 100)
                    {
                        if(speechSpeed < 5)
                        {
                            speechSpeed++;
                        }
                        string cpuLoadVocalMessage = cpuMaxedOutMessage[rand.Next(4)];
                        JoshSpeak(cpuLoadVocalMessage,VoiceGender.Male, speechSpeed++);
                    }
                    else
                    {
                        string cpuLoadVocalMessage = String.Format("The current CPU load is {0} percent", currentCpuPercentage);
                        JoshSpeak(cpuLoadVocalMessage, VoiceGender.Male, 2);
                    }
                    string memAvailableVocalMessage = String.Format("You currently have {0} megabytes of memory available", currentAvailableMemory);
                    JoshSpeak(memAvailableVocalMessage, VoiceGender.Male, 2);
                string tempVocalMessage = String.Format("You computer's current temperature is {0} degrees", currentTemp);
                JoshSpeak(tempVocalMessage, VoiceGender.Male, 2); 
                Thread.Sleep(1000);
            }
        }
        public static void JoshSpeak(string message, VoiceGender voiceGender)
        {
            synth.SelectVoiceByHints(voiceGender);
            synth.Speak(message);
        }
        public static void JoshSpeak(string message, VoiceGender voiceGender, int rate)
        {
            synth.Rate = rate;
            JoshSpeak(message, voiceGender);
        }
    }
}

错误输出如下:

代码:CS1009描述:无法识别的转义序列第35行

如何在c# . net中使用温度性能监视器

变化

PerformanceCounter perfTempZone = new PerformanceCounter("Thermal Zone Information", "Temperature", "'_TZ.TZ00");

,@"'_TZ.TZ00"代替"'_TZ.TZ00"

编译器将反斜杠解释为转义序列的开始。你可以告诉它在字符串前面加上@来接收输入。