【发布时间】:2012-03-06 13:41:17
【问题描述】:
我必须使用某种技术来获得系统运行时间。由 StackOverflow 中的某个人建议,我使用性能计数器来获取 TimeSpan 中的系统正常运行时间。
try
{
var uptime = new PerformanceCounter("System", "System Up Time");
uptime.NextValue();
return TimeSpan.FromSeconds(uptime.NextValue());
}
catch (Exception)
{
}
它在我的电脑上完美运行,没有任何问题。但是我朋友的电脑出现了问题。在他的 PC 上,运行以下行时抛出异常。
var uptime = new PerformanceCounter("System", "System Up Time");
例外是"Input String was not in Correct format"。
【问题讨论】:
-
不,PerformanceCounter 不会引发 FormatException。考虑删除 try/catch 关键字。
-
@HansPassant,我也相信,直到我发现这个异常,我尝试删除 try/catch,我的应用程序冻结并显示“没有响应”......
标签: c# performancecounter