为什么DateTime.Subtract有时返回真正的奇数值?
private ConcurrentDictionary<string, Request> _waitingList = new ConcurrentDictionary<string, Request>();
// send requests in thread #1 and set their SentAt time
_waitingList.TryAdd(macAddress, request);
request.SentAt = DateTime.UtcNow;
// then in a timer tick event in different thread every 5 seconds
private void OnTimerTick(object sender, ElapsedEventArgs e) {
foreach (var waiting in _waitingList) { // ConcurrentDictionary<string, Request>
var request = waiting.Value;
var msSinceSent = DateTime.UtcNow.Subtract(request.SentAt).TotalMilliseconds;
Debug.WriteLine($"sent *{request.Type}* {msSinceSent} ms ago");
}
}
sent *PackageUpdate* 336.2019 ms ago sent *PackageUpdate* 698.1145 ms ago sent *PackageUpdate* 1059.864 ms ago sent *PackageUpdate* 63693095680615.5 ms ago
最后一个价值是巨大的,我经常得到这些价值,甚至不知道这怎么可能。
答案 0 :(得分:3)
如果request.SentAt
被设置为DateTime.MinValue
,则将获得该值。我认为您的数据存储区中的值是null或零,并且正在某个地方转换为DateTime.MinValue
。
例如,此代码:
Console.WriteLine(DateTime.UtcNow.Subtract(DateTime.MinValue).TotalMilliseconds);
将输出此值:
63693097183442.7