Solved

foreach fatal error problem

Posted on 2010-09-21
12
283 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 74

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 74

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now