在我的.net域對象中我正在跟蹤每個狀態轉換。這是通過將狀態設置爲狀態歷史記錄集合來完成的。所以稍後,可以看到一個desc的有序列表,找出其中的狀態在更改了什麼時間。.Net DateTime精度
所以有這樣的方法:
private void SetState(RequestState state)
{
var stateHistoryItem = new RequestStateHistoryItem(state, this);
stateHistoryItems.Add(stateHistoryItem);
}
當一個新RequestStateHistoryItem
被實例化,當前的日期將自動分配。像這樣:
protected IdentificationRequestStateHistoryItem()
{
timestamp = EntityTimestamp.New();
}
的EntityTimestamp
對象是含有此時,相應的用戶和創建和改變日期的對象。
當列出的狀態歷史,我做了降序使用LINQ:
public virtual IEnumerable<RequestStateHistoryItem> StateHistoryItems
{
get { return stateHistoryItems.OrderByDescending(s => s.Timestamp.CreatedOn.Ticks); }
}
現在,當一個新的Request
被實例化的第一狀態Received
是在構造函數SetState(RequestState.Received)
設置。然後,沒有任何延遲並取決於某些條件,設置新狀態Started
。經過一段時間(數據庫操作)後,設置了狀態Finished
。
現在執行降序排序時,Received
總是在Started
狀態之後。當我慢慢地調試時,或者在將狀態設置爲Started
之前放置System.Threading.Thread.Sleep(1000)
時,排序工作。 如果沒有,如上所述,Started
狀態的CreatedOn是OLDER然後Received
CreatedOn date ?!
TimeOfDay {17:04:42.9430318} FINSHED
Ticks 634019366829430318
TimeOfDay {17:04:39.5376207} RECEICED
Ticks 634019366795376207
TimeOfDay {17:04:39.5367815} STARTED
Ticks 634019366795367815
這怎麼可能?我會理解,如果收到的和開始日期完全相同,但我不明白它如何能在另一個之前?
我已經嘗試new DateTimePrecise().Now,
(請參閱DateTimePrecise class)我發現在另一個問題。同樣的結果。
任何人都知道這可能是什麼?
更新
public virtual bool Finish()
{
// when I put the SetState(State.Received) from the constructor into here, the timestamp of finish still is BEFORE received
SetState(IdentificationRequestState.Received);
SetState(IdentificationRequestState.Finished);
// when I put the SetState(State.Received) after Finished, then the Received timestamp is BEFORE Finished
SetState(IdentificationRequestState.Finished);
SetState(IdentificationRequestState.Received);
var match = ...
if (match != null)
{
...
}
else
{
...
}
}
你在多線程環境中運行?如果接收和啓動狀態是由不同線程請求的,這可以解釋您看到的差異。 – 2010-02-16 16:42:38