Solved

foreach fatal error problem

Posted on 2010-09-21
12
286 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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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