简体   繁体   English

如何在C#.NET中使用温度性能监视器?

[英]How do I use the Temperature Performance Monitor in C#.NET?

I'm trying to create an application using speech for the blind and visually impaired that tells you the system uptime, how much RAM you have available, current CPU load, and other stuff like that, and I ran into an issue; 我正在尝试使用针对盲人和视障者的语音创建一个应用程序,该应用程序会告诉您系统正常运行时间,可用的RAM数量,当前的CPU负载以及类似的其他内容,但我遇到了问题; I cannot get the Temperature Performance Counter to work properly 我无法使温度性能计数器正常工作

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);
        }
    }
}

The error output is as follows: 错误输出如下:

Code: CS1009 Description: Unrecognized escape sequence Line 35 代码:CS1009说明:无法识别的转义序列第35行

Change 更改

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

with @"\\_TZ.TZ00" instead of "\\_TZ.TZ00" @"\\_TZ.TZ00"代替"\\_TZ.TZ00"

The compiler interprets the backslash as starting an escape sequence. 编译器将反斜杠解释为开始转义序列。 You can tell it to take the input literally with @ in front of the string. 您可以告诉它在字符串前面使用@来按字面意义接受输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM