已經進行了一些搜索,但無法找到我在這裏面臨的相同情況。訪問修改後的關閉問題
這裏是我擔心的片段:
var threadsafeVacantStats = new Dictionary<Thread, StatsNode>();
var threadSafeVacantLocker = new Object();
var threadsafeZoneStats = new Dictionary<Thread, StatsNode>();
var threadSafeZoneLocker = new Object();
Parallel.ForEach(m_TeamAreasByZone[zone.ContainerID], team =>
{
var tempVacantStatNode = CreateASyncStatsNodes(threadSafeVacantLocker, threadsafeVacantStats);
var tempZoneStatNode = CreateASyncStatsNodes(threadSafeZoneLocker, threadsafeZoneStats);
//...other stuff
}
下面是它的調用函數:
private static StatsNode CreateASyncStatsNodes(object threadSafeLocker, Dictionary<Thread, StatsNode> threadsafeTeamStats)
{
StatsNode tempStatsNode;
var currentThread = Thread.CurrentThread;
lock (threadSafeLocker)
{
if (!threadsafeTeamStats.ContainsKey(currentThread))
threadsafeTeamStats[currentThread] = new StatsNode(0, 0);
tempStatsNode = threadsafeTeamStats[currentThread];
}
return tempStatsNode;
}
對我來說,這看起來不錯,但ReSharper的是給了第二個電話警告創建ASyncStatsNodes(第一個調用很好)。 繼建議,它把塊到:
var threadsafeVacantStats = new Dictionary<Thread, StatsNode>();
var threadSafeVacantLocker = new Object();
var threadsafeZoneStats = new Dictionary<Thread, StatsNode>();
var threadSafeZoneLocker = new Object();
object locker = threadSafeZoneLocker;
Dictionary<Thread, StatsNode> stats = threadsafeZoneStats;
Parallel.ForEach(m_TeamAreasByZone[zone.ContainerID], team =>
{
var tempVacantStatNode = CreateASyncStatsNodes(threadSafeVacantLocker, threadsafeVacantStats);
var tempZoneStatNode = CreateASyncStatsNodes(locker, stats);
//...
}
這並沒有真正意義的我,似乎並沒有真正解決什麼(如果它被擺在首位,甚至損壞) 。是resharper錯誤地標記給出警告,還是我錯過了什麼?
您正在使用哪個版本的R#?我沒有使用R#5.0獲得上述代碼的警告。 – adrianbanks 2011-01-12 23:41:43
它的版本5.1 – Rob 2011-01-12 23:44:52