我是否正确地假设此逻辑(在注释中说明)不正确?
// 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的MSDN文档后,在我看来应该比较合适的参数是1或0以实现此功能,但是这段代码已经存在了很多年。
最佳答案
在功能上等效于:
while(now < finishTime);
看来它将正常工作。每当它达到0时,当前时间等于finishTime,每当达到1时,当前时间都大于finishTime。无论哪种情况,循环都会结束。
更好的方法是:
while(DateTime.Now < this._finishTime)
这与
TimeOfDay
无关,它会每天产生一个问题(正如@AakashM在评论中指出的那样)。
关于c# - 时间跨度比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8096044/