【发布时间】:2011-12-02 13:15:11
【问题描述】:
我有以下 C# 代码:
1. List<BandEdge> bandEdgeList;
2.
3. bandEdgeList = CicApplication.BandEdgeCache.Where(row => row.Coater == coater).ToList();
4. foreach (BandEdge bandEdge in bandEdgeList)
5. {
6. ...
7. ...
8. }
我的问题是这样的。一旦在第 3 行填充了“bandEdgeList”,如果另一个线程修改了 CicApplication.BandEdgeCache 的内容,“bandEdgeList”的内容是否会失效?我锁定了 CicApplication.BandEdgeCache getter/setter。但我想知道是否应该锁定这段代码,以便在使用“bandEdgeList”时 CicApplication.BandEdgeCache 的内容不会改变。
【问题讨论】:
-
如果
BandEdgeCache也是一个可编辑的集合,那么您可能实际上会在本地列表中丢失项目,可能有项目不满足您的Coater条件,或者可能在您的列表中除了下面的答案中的问题之外,它们不再在BandEdgeCache集合中。
标签: c# multithreading concurrency locking