我假设这个逻辑(在注释中解释)是不正确的,对吗?
// While the current time is before the finish time, sleep the current thread.
// The timer will continue to run the getting of the rates
while (TimeSpan.Compare(DateTime.Now.TimeOfDay,this._finishTime.TimeOfDay) == -1)看过TimeSpan.Compare的MSDN文档后,我似乎认为应该在正确的参数为1或0时才能实现此功能,但这段代码已经存在多年了。
发布于 2011-11-11 23:21:47
这在功能上等同于:
while(now < finishTime);它看起来会正常工作。每当它达到0时,当前时间等于finishTime,每当它达到1时,当前时间大于finishTime。无论哪种情况,循环都将结束。
更好的方法是:
while(DateTime.Now < this._finishTime)这并不关心TimeOfDay,因为它每天都在制造问题(正如@AakashM在评论中指出的那样)。
发布于 2011-11-11 23:22:25
嗯,它实际上是在等待完成时间,而现在的时间是<。
所以是的,当它是0或者1的时候,它会退出循环,但是当它是-1的时候,它会循环。,,,,,,it,‘s,it,’s,‘s,it,’s,‘s,’s,
https://stackoverflow.com/questions/8096044
复制相似问题