System.Diagnostics.Stopwatch返回已用负数...性质 [英] System.Diagnostics.Stopwatch returns negative numbers in Elapsed... properties
问题描述
这是正常行为秒表可以返回负值? code下面的示例可用于重现。
Is it normal behaviour that Stopwatch can return negative values? Code sample below can be used to reproduce it.
while (true)
{
Stopwatch sw = new Stopwatch();
sw.Start();
sw.Stop();
if (sw.ElapsedMilliseconds < 0)
Debugger.Break();
}
在这里我可以重现负数的唯一的地方就是我的虚拟机(Hyper-V的8核的机器上托管)
The only place where I can reproduce negative numbers is my virtual machine (hosted by Hyper-V on a 8-core machine)
推荐答案
这是一个<一个href=\"https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94083&wa=wsignin1.0\">bug.它似乎并不有很多的注意周围通过,所以我建议用该报告的后续行动。
This is a bug. It doesn't seem to have a lot of attention around it, through, so I'd suggesting following up with that report.
的<一个href=\"https://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=94083\">uninspiring 似乎是解决方法忽略负值:
The uninspiring workaround appears to be to ignore negative values:
long elapsedMilliseconds = Math.Max(0, stopwatch.ElapsedMilliseconds);
这篇关于System.Diagnostics.Stopwatch返回已用负数...性质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!