Solved

foreach fatal error problem

Posted on 2010-09-21
12
289 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 250 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 250 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

719 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