惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
C
Cisco Blogs
博客园 - 叶小钗
P
Privacy International News Feed
Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
IT之家
IT之家
博客园 - 聂微东
Know Your Adversary
Know Your Adversary
Help Net Security
Help Net Security
罗磊的独立博客
I
Intezer
S
Schneier on Security
博客园_首页
C
CERT Recently Published Vulnerability Notes
雷峰网
雷峰网
Cisco Talos Blog
Cisco Talos Blog
宝玉的分享
宝玉的分享
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Webroot Blog
Webroot Blog
TaoSecurity Blog
TaoSecurity Blog
MyScale Blog
MyScale Blog
P
Privacy & Cybersecurity Law Blog
T
The Exploit Database - CXSecurity.com
PCI Perspectives
PCI Perspectives
Security Latest
Security Latest
H
Heimdal Security Blog
S
Secure Thoughts
Hacker News: Ask HN
Hacker News: Ask HN
Y
Y Combinator Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Microsoft Security Blog
Microsoft Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
SecWiki News
SecWiki News
The GitHub Blog
The GitHub Blog
A
Arctic Wolf
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
T
Threat Research - Cisco Blogs
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 徘徊中的海鸟

ADO.NET,连接\连接池问题 - 徘徊中的海鸟 - 博客园 关于sql server占用系统资源的问题 ASP.NET 的内存不足问题 SQL拆分字段 生成代码自动加入到解决方案中 SQL Server 2005数据库日志文件损坏的情况下如何恢复数据库 .net 发现的cache 问题 .NET线程若干问题 HttpContext.Cache 和 HttpRuntime.Cache IIS 6.0中配置HTTP Gzip压缩 pre标签自动换行方案 HttpWebRequest 和 浏览器打开的区别 TestDirector 安装、使用心得 连接数问题 w3wp 进程问题 关于CultureInfo 读取树数据SQL 提高sql的效率 SQL中只获取日期值
在Dictionary使用foreach的注意
徘徊中的海鸟 · 2007-10-26 · via 博客园 - 徘徊中的海鸟

   最近在对博客园的程序进行性能优化,在优化过程中用到了Dictionary

,在通过foreach将Dictionary中的数据写入数据库时,遇到了这样的错误:
     Collection was modified; enumeration operation may not execute.
     
     代码类似这样的:    

Dictionary<intint> _dictionary = new Dictionary<intin>();
//添加数据操作省略
foreach(KeyValuePair<intint> item in _dictionary)
 {
 }


     在执行foreach时,其他线程对_dictionary进行了Add操作,改变了_dictionary中的数据,从而产生了上述的异常信息。

     那什么会产生这样的异常信息呢?
     foreach实际上执行的代码是:    

Dictionary<intint>.Enumerator enumerator = _dictionary.GetEnumerator(); 
try  

   
while (enumerator.MoveNext())  
   { 
     
    } 

finally  

   IDisposable d 
= enumerator as IDisposable; 
   
if (d != null) d.Dispose(); 
}


     通过Reflector查看Dictionary<TKey, TValue>.Enumerator.MoveNext()源代码,我们会发现开始处有这样的代码:

if (this.version != this.dictionary.version)
      {
            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
      }

     而异常就是在这里发生的,因为Add操作时改变了Dictionary的version,通过查看Insert(TKey key, TValue value, bool add)的源代码会看出。
     我觉得Dictionary<TKey, TValue>应该提供一个方法,可以设置在MoveNext时是否进行版本检查,因为有时在foreach操作时,可能并不关心Dictionary中的数据是否被修改,我遇到的就是这样的情况,现在由于这个问题而不能使用foreach,而只能采取其他方法遍历Dictionary<TKey, TValue>中的数据。
     我采用的方法是:

Dictionary<intint> _dictionary = new Dictionary<intin>();
//添加数据操作省略
int[] dataArray = new int[_dictionary.Values.Count];
_ dictionary.Values.CopyTo(evcArray, 
0)
for (int i = 0; i < dataArray.Length; i++)
  {
   }


第2种方法在读取的时候上锁。
  lock(tcClientList.SyncRoot)  
  {  
  foreach   (TcClient   tcClient   in   tcClientList)  
  ...  
  }