First of all, at the beginning of my code I declare an ArrayList public ArrayList ArrivalsInApp = new ArrayList();
. Then, much much later, I am downloading some data from XML file. For each XmlNode called "flight" I am creating a Panel.
While creating the Panel, I add its name into the ArrivalsInApp
ArrayList. The generating of the panel is in a method. The method has several foreach cycles and IF conditions. Technically, all it does it decides if Panel should be created (if its in XML and not in app), updated (in in both) or deleted (in app but not in XML).
In the method, I get names of all flights in XML, save into ArrayList. Now using foreach, I check if all flights in the application (so in the ArrivalsInApp ArrayList) are ALSO in the XML. I do this using this code:
foreach (string y in ArrivalsInApp)
{
if (XmlArrivals.Contains(y) == false)
{
*code*
}
}
The "deciding" method is called on form1_load, and every 30 second using a Timer. When the timer reaches specified value, I reload the XML and then call the method. But, I get an InvalidOperationException with comment that "Collection was modified; enumeration operation may not execute." at line foreach (string y in ArrivalsInApp)
. I am totally desperate, I have no Idea what causes this.
What happens if I run the .exe file (if I just debug it, after the timer reaches the value, the program shuts down and shows this error) is, that the Panel is deleted regardless if it should be updated or deleted, and the unhandled exception error pops up. Sorry if the explanation is a bit confused. If anyone would like me to post the whole source file (maybe rather the whole project) just let me know. However, since it has now about 1800 lines of code, it would need quite a lot of explanation.