gpt4 book ai didi

C# 在使用 TimeSpan 时遇到了麻烦。将正常运行时间秒数转换为天/小时

转载 作者:行者123 更新时间:2023-12-04 17:17:57 25 4
gpt4 key购买 nike

我正在尝试编写一个非常基本的程序来检查我的 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 中做错了什么。我对所有这一切都非常陌生所以如果我问这个问题听起来很愚蠢,我深表歉意。

感谢您提供的任何帮助。

最佳答案

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

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

关于C# 在使用 TimeSpan 时遇到了麻烦。将正常运行时间秒数转换为天/小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28331729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com