none
Collection was modified; enumeration operation may not execute RRS feed

  • Question

  • "System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute." error occur from following code.

    public void CheckGarbageReminderScheduleItem(List<Appointment> appointmentlist, List<ReminderSchedule> reminderscheduelist) { bool exist; foreach (ReminderSchedule reminderschedule in reminderscheduelist) { exist = false; foreach (Appointment appointment in appointmentlist) { if (reminderschedule.Appointment.ApptNo == appointment.ApptNo) { exist = true; break; }

    } if (exist == false) { DeleteReminderScheduleDB(reminderschedule.Appointment.ApptNo); reminderschedule.Stop(); reminderscheduelist.Remove(reminderschedule); } }

    }

    ReminderSchedule class is inherited from System.Windows.Forms.Timers and remindschedulelist is List<ReminderSchedule>.

    And this method is called like this.

    CDMrCommon.Instance.CheckGarbageReminderScheduleItem(appointmentlist, reminderschedulelist);

    CDMrCommon.Instance is a static method like following.

    public static CDMrCommon Instance
    {
        get
        {
            if (m_CDMrCommon != null)
            {
                return m_CDMrCommon;
            }
            else
            {
                m_CDMrCommon = new CDMrCommon();
                return m_CDMrCommon;
            }
        }
    }
    Can anybody give me some advice about this error?

    • Edited by Jeff0803 Thursday, May 9, 2019 4:43 AM
    Thursday, May 9, 2019 4:39 AM

Answers

  • You can not change the content of the list while you are using IEnumerator (forearch) to iterate it. Modification happens in your code at line

    reminderscheduelist.Remove(reminderschedule);

    One approach is to keed record what items needs to be removed from list and remove them after for each loop. Then method would look like this

    public void CheckGarbageReminderScheduleItem(List<Appointment> appointmentlist, List<ReminderSchedule> reminderscheduelist) { bool exist; List<ReminderSchedule> removedReminders = new List<ReminderSchedule>(); foreach (ReminderSchedule reminderschedule in reminderscheduelist) { exist = false; foreach (Appointment appointment in appointmentlist) { if (reminderschedule.Appointment.ApptNo == appointment.ApptNo) { exist = true; break; } } if (exist == false) { DeleteReminderScheduleDB(reminderschedule.Appointment.ApptNo); reminderschedule.Stop(); //reminderscheduelist.Remove(reminderschedule); removedReminders.Add(reminderschedule); } } foreach (ReminderSchedule removeReminder in removedReminders) reminderscheduelist.Remove(removeReminder); }


    Thursday, May 9, 2019 4:46 AM

All replies

  • You can not change the content of the list while you are using IEnumerator (forearch) to iterate it. Modification happens in your code at line

    reminderscheduelist.Remove(reminderschedule);

    One approach is to keed record what items needs to be removed from list and remove them after for each loop. Then method would look like this

    public void CheckGarbageReminderScheduleItem(List<Appointment> appointmentlist, List<ReminderSchedule> reminderscheduelist) { bool exist; List<ReminderSchedule> removedReminders = new List<ReminderSchedule>(); foreach (ReminderSchedule reminderschedule in reminderscheduelist) { exist = false; foreach (Appointment appointment in appointmentlist) { if (reminderschedule.Appointment.ApptNo == appointment.ApptNo) { exist = true; break; } } if (exist == false) { DeleteReminderScheduleDB(reminderschedule.Appointment.ApptNo); reminderschedule.Stop(); //reminderscheduelist.Remove(reminderschedule); removedReminders.Add(reminderschedule); } } foreach (ReminderSchedule removeReminder in removedReminders) reminderscheduelist.Remove(removeReminder); }


    Thursday, May 9, 2019 4:46 AM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 13, 2019 9:00 AM
    Moderator