Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

foreach fatal error problem

Posted on 2010-09-21
12
Medium Priority
?
291 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 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