InterlockManager的Monitor()方法产生“集合已修改;可能无法执行枚举操作。”异常。 #1

Closed
opened 2023-05-13 09:48:32 +08:00 by liang.su · 1 comment
Owner

10号机RT输出如下日志:

2023-05-13 09:20:46.751
System.InvalidOperationException: 集合已修改;可能无法执行枚举操作。
   在 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   在 System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
   在 Aitex.Core.RT.IOCore.InterlockManager.Monitor() 位置 D:\Sicentury\SIC.Framework\MECF.Framework.Common\Aitex\Core\RT\IOCore\Interlock\InterlockManager.cs:行号 195
   在 MECF.Framework.Common.IOCore.IoManager.OnTimer() 位置 D:\Sicentury\SIC.Framework\MECF.Framework.Common\MECF\Framework\Common\IOCore\IoManager.cs:行号 68
10号机RT输出如下日志: ``` 2023-05-13 09:20:46.751 System.InvalidOperationException: 集合已修改;可能无法执行枚举操作。 在 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) 在 System.Collections.Generic.Dictionary`2.Enumerator.MoveNext() 在 Aitex.Core.RT.IOCore.InterlockManager.Monitor() 位置 D:\Sicentury\SIC.Framework\MECF.Framework.Common\Aitex\Core\RT\IOCore\Interlock\InterlockManager.cs:行号 195 在 MECF.Framework.Common.IOCore.IoManager.OnTimer() 位置 D:\Sicentury\SIC.Framework\MECF.Framework.Common\MECF\Framework\Common\IOCore\IoManager.cs:行号 68 ```
Author
Owner
  • 该问题导致的原因
    Monitor()方法已在后台线程中启动执行,而互锁表还未创建完成,CreateInterlockLimit()方法修改了_dicLimitToActionMap字典;此时Monitor()正在foreach中遍历该字典,导致抛出此异常。

  • 解决方法
    Monitor()中遍历_dicLimitToActionMap前,先使用ToArray()方法创建KeyValuePair副本。

参考:
[1] https://stackoverflow.com/a/3875401/1957479
[2] https://languagelassi.blogspot.com/2014/11/Collection-was-modified-enumeration-operation-may-not-execute.html
[3] https://stackoverflow.com/a/604843

- 该问题导致的原因 ```Monitor()```方法已在后台线程中启动执行,而互锁表还未创建完成,```CreateInterlockLimit()```方法修改了```_dicLimitToActionMap```字典;此时Monitor()正在foreach中遍历该字典,导致抛出此异常。 - 解决方法 ```Monitor()```中遍历```_dicLimitToActionMap```前,先使用ToArray()方法创建KeyValuePair副本。 参考: [1] https://stackoverflow.com/a/3875401/1957479 [2] https://languagelassi.blogspot.com/2014/11/Collection-was-modified-enumeration-operation-may-not-execute.html [3] https://stackoverflow.com/a/604843
liang.su added the
bug
label 2023-05-13 10:05:52 +08:00
liang.su referenced this issue from a commit 2023-05-13 10:14:08 +08:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Sic-Software/Sic.Framework#1
No description provided.