c# - 时间跨度比较

标签 c# .net timespan

我是否正确地假设此逻辑(在注释中说明)不正确?

// 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/

相关文章:

c# - 在 .Net 中编译为 native 代码是否会完全删除 MSIL?

c# - 创建安全字符串以连接到 SQL Server

c# - 格式化 TimeSpan 字符串

c# - wpf mvvm绑定(bind)时间跨度,只显示分钟:seconds?

java - 为什么 Java/Scala 使用 extends/implements 关键字?

c# - 使用 yield WaitForSeconds

c# - Windows Phone 8 的 WebResponse 是否跳过 set-cookie header

c# - default(T) 返回 null 其中 T 继承自 DataContext

c# - 如何将 TimeSpan 舍入为一位数毫秒

c# - 将 Firefox 嵌入到 .NET 控件中