【发布时间】:2013-10-17 14:56:34
【问题描述】:
我定义了一个 SortedDictionary:
private SortedDictionary<int, SingleQcCalculation> m_dicTagsToCalc;
我正在迭代它并在值内设置一个属性:
foreach (KeyValuePair<int, SingleQcCalculation> pairSingleCalc in m_dicTagsToCalc)
{
try
{
pairSingleCalc.Value.m_QCtoCalc.m_CurGroupID = pairSingleCalc.Value.m_ChangedQcGroupId;
pairSingleCalc.Value.m_QCtoCalc.CalculateQCExpression(pairSingleCalc.Value.m_OPCChangeTime, pairSingleCalc.Value.m_calcTime);
}
catch (Exception ex)
{
logger.Error(LogTopicEnum.DA,"Error calculating calced QC (id=" + pairSingleCalc.Value.m_QCtoCalc.ID.ToString() + ")" , ex);
}
}
由于某种原因,我得到了这个异常:
DoQCsCalculations: error while calculating calced tags
System.InvalidOperationException: Collection was modified after the enumerator was instantiated.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.SortedSet`1.Enumerator.MoveNext()
现在,我没有更改字典中的值或键,没有 其他线程使用字典。
为什么会出现这个错误?
谢谢, 奥马尔
【问题讨论】:
-
你在什么属性上排序?
-
@RobG ,SortedDictionary 按键排序,我正在使用此默认排序行为。
-
@VladimirFrolov ,我没有更改值,只是其中的属性
-
这必须有效。至少在达到 CalculateQCExpression 之前。你一步步调试过这个吗? CalculateQCExpression 中有什么?
-
看,如果被您提供的代码示例捕获,抛出的异常消息不是应该显示的消息:它不同于“calced QC (id="。它是由计算QCExpression ?
标签: c# .net dictionary enumeration sorteddictionary