簡體   English   中英

如何在C#.NET中使用溫度性能監視器?

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

我正在嘗試使用針對盲人和視障者的語音創建一個應用程序,該應用程序會告訴您系統正常運行時間,可用的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行

更改

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

@"\\_TZ.TZ00"代替"\\_TZ.TZ00"

編譯器將反斜杠解釋為開始轉義序列。 您可以告訴它在字符串前面使用@來按字面意義接受輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM