?
Solved

foreach fatal error problem

Posted on 2010-09-21
12
Medium Priority
?
290 Views
Last Modified: 2013-11-07
Hi EE,
I have this method in the code which causes a fatal error in log4net occasionally.  The error message is below, can anyone see how I can handle this?  I could include better error messaging for next time, if or when it happens again but I would prefer to correc the code to stop it happening.  We are using Castle for configuration so I could possibly use a lifestyle setting of transient but I dont know why this is happening really...  Im guessing maybe another thread is using the same code but I dont fully understand why this causes a problem because I'm not removing anything from the list.

any ideas?

Thanks,

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.     at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)     at System.Collections.Generic.List`1.Enumerator.MoveNextRare()     at System.Collections.Generic.List`1.Enumerator.MoveNext()    
public List<ABC> TargetList(IList<Car> cars)
        {
            var itemList = new List<ABC>();
            foreach (Car car in cars)
            {
                itemList.Add(MapFromDepot(car));
            }
            return itemList;
        }

Open in new window

0
Comment
Question by:jimbona27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 33724723
I think that, outside this procedure, you're modifying your cars collection at the same time that this procedure executes. Note that you're using cars here by reference, not by value, so anywhere in your code you're removing/adding items to cars collection.

Try to clone your collection just before calling TargetList and passing the cloned collection instead the original.

Hope that helps.
0
 

Author Comment

by:jimbona27
ID: 33724831
great, just looking for examples now.......
thanks,
0
 

Author Comment

by:jimbona27
ID: 33724915
just thinking out loud..
the class file in castle config has a lifestyle of perwebrequest so why whould another thread be causing the problem if it had its own instance?

Thanks,
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:jimbona27
ID: 33725065
can I have an example please?
thanks,
0
 
LVL 25

Assisted Solution

by:Luis Pérez
Luis Pérez earned 1000 total points
ID: 33725118
Try locking your cars collection while doing the foreach:
public List<ABC> TargetList(IList<Car> cars)
        {
            var itemList = new List<ABC>();
            lock (cars) {
                foreach (Car car in cars)
                {
                    itemList.Add(MapFromDepot(car));
                }
            }
            return itemList;
        }
0
 

Author Comment

by:jimbona27
ID: 33725149
what will this mean when there is a conflict between threads?
0
 

Author Comment

by:jimbona27
ID: 33725198
just looked on msdn.. i dont think lock is something I can use as its not a vital part of taking payment or something... i need it to handle itself quickly and no waiting..  

0
 

Author Comment

by:jimbona27
ID: 33725903
can I have an example of cloning this function?
Thanks,
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33779836
What does MapFromDepot do? If it does any modification to the incoming Car object, then you will get this error. The simple answer would be to use a for loop instead of a foreach any time you are modifying items in a collection.
public List<ABC> TargetList(IList<Car> cars)
{
	var itemList = new List<ABC>();
	
	for (int i = 0; i < itemList.Count; i++)
	{
		itemList.Add(MapFromDepot(itemList[i]));
	}
	
	return itemList;
}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 33779846
Correction--wrong list  :/
public List<ABC> TargetList(IList<Car> cars)
{
	var itemList = new List<ABC>();
	
	for (int i = 0; i < cars.Count; i++)
	{
		itemList.Add(MapFromDepot(cars[i]));
	}
	
	return itemList;
}

Open in new window

0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34026561
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question