2015-02-04 20 views
2

我正試圖編寫一個非常基本的程序來檢查我的cpu和RAM的使用情況,並告訴我當前的正常運行時間。如果CPU或RAM超過一定的閾值它會口頭宣佈它並警告我。我正在嘗試使用TimeSpan自動將系統正常運行時間從幾秒鐘更改爲幾天,幾小時,幾分鐘,幾秒。當我運行代碼時,它表示0天0小時0分鐘和0秒。但是,如果我不使用TimeSpan,它會在幾小時內正確地宣佈它。C#遇到TimeSpan的麻煩。將正常運行時間秒數轉換爲天/小時

#region My Performace Counters 
     //this pulls the current cpu load in % 
     PerformanceCounter perfCpuCount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total"); 

     //this pulls the available memory in MBytes 
     PerformanceCounter perfAvailableMemoryCount = new PerformanceCounter("Memory", "Available MBytes"); 

     //this shows the systems uptime (in seconds) 
     PerformanceCounter perfUptimeCount = new PerformanceCounter("System", "System Up Time"); 
     #endregion 

     TimeSpan uptimeSpan = TimeSpan.FromSeconds(perfUptimeCount.NextValue()); 
     string systemUptimeMessage = string.Format("The current system uptime is {0} days {1} hours {2} minutes and {3} seconds", 
      uptimeSpan.TotalDays, 
      uptimeSpan.TotalHours, 
      uptimeSpan.TotalMinutes, 
      uptimeSpan.TotalSeconds 
      ); 

     Console.WriteLine("{0}", systemUptimeMessage); 


     //tell the user what the current system uptime is 
     synth.Speak(systemUptimeMessage); 

一旦我運行那部分,它只是告訴我0,0,0,0天,小時,分鐘和秒。但是,如果我繼續下一個部分,我可以在幾小時內成功完成系統的正常運行。

//infinte While loop 
     while(true) 
     { 
      //get the current performance values 
      int currentCpuPercentage = (int)perfCpuCount.NextValue(); 
      int currentAvailableMemory = (int)perfAvailableMemoryCount.NextValue()/1024; 
      int currentUptime = (int)perfUptimeCount.NextValue()/3600; 

      //Every 1 Second prints the cpu load (%) and available mem (GB) to screen 
     Console.WriteLine("Cpu Load  : {0}%", currentCpuPercentage); 
     Console.WriteLine("Available Memory: {0} Gigabytes", currentAvailableMemory); 
     Console.WriteLine("System Uptime : {0} hours", currentUptime);   


      //if cpu is over 80 percent, vocally say the current percent 
      if(currentCpuPercentage > 80) 
      { 
       if (currentCpuPercentage == 100) 
       { 
        string cpuLoadVocalMessage = String.Format("You're CPU is about to catch fire!"); 
        synth.Speak(cpuLoadVocalMessage); 
       } 

       else 

       { 
        string cpuLoadVocalMessage = String.Format("The current CPU load is {0} percent", (int)currentCpuPercentage); 
        synth.Speak(cpuLoadVocalMessage); 
       } 
      } 

      //if available memory is less than 1 vocally say the current available memory 
      if(currentAvailableMemory < 1) 
      { 
       string memAvailVocalMessage = String.Format("The current available Memory is {0} Gigabytes", (int)currentAvailableMemory); 
       synth.Speak(memAvailVocalMessage); 
      } 





     Thread.Sleep(1000); 

      //just an extra space 
     Console.WriteLine(); 

      //end of loop 
     } 

我不打算保持正常運行時間消息中的While循環,我只是離開它那裏檢查,如果我得到正確的時間(我)。我可以很容易地將它保持原樣,只需在3600秒內跳過幾秒就可以檢查它幾個小時,但我只想了解我在TimeSpan中做錯了什麼。我對這一切都非常陌生所以我很抱歉,如果我聽起來啞巴問這個。

感謝您提供任何幫助。

回答

0

這是計數器的預期行爲,第一次調用將返回0.0。請參閱文檔https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue(v=vs.110).aspx

您可以通過向NextValue()添加一個附加(初始)調用來解決此問題。

+0

謝謝。我想我已經明白了這一點。如果我理解正確,那麼在我試圖將它放入TimeSpan之前,我需要調用下一個值。現在它至少在工作,所以我認爲這是正確的大聲笑。再次感謝。 – dusty24