C# 运行 陷入 TimeSpan 的麻烦。将正常运行时间秒数转换为 days/hours

C# running into troubles with TimeSpan. Converting uptime seconds to days/hours

我正在尝试编写一个非常基本的程序来检查我的 cpu 和 ram 使用情况并告诉我当前的正常运行时间。如果 cpu 或 ram 超过某个阈值,它会口头宣布并警告我。我正在尝试使用 TimeSpan 自动将我的系统正常运行时间从秒变为天、小时、分钟、秒。当我 运行 代码时,它显示 0 天 0 小时 0 分 0 秒。但是,当我不使用 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 中做错了什么。我对所有这一切都非常陌生所以如果我问这个问题听起来很愚蠢,我深表歉意。

感谢您提供的任何帮助。

这是计数器的预期行为,第一次调用将 return 0.0。请参阅 https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue(v=vs.110).aspx

上的文档

您可以通过向 NextValue() 添加一个额外的(初始)调用来解决此问题。