Click here to Skip to main content
15,916,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Iam getting this error, i couldnt solve this problem, Kindly help me out from this issue.
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()


C#
<pre>  List<string> AppList = new List<string>();

  public void loadApplicationDetail()
{
AppList.Clear();
                var Selectlistapp = Dbl.ReturnDataset("select Address,Aort from tblor_Details");
                if (Selectlistapp.Tables[0].Rows.Count > 0)
                {
                    for (var i = 0; i < Selectlistapp.Tables[0].Rows.Count; i++)
                    {
                        string Address = Selectlistapp.Tables[0].Rows[i]["Address"].ToString();
                        string Aort = Selectlistapp.Tables[0].Rows[i]["Aort"].ToString();
                        AppList.Add("http://" + Address + ":" + Aort + "/");
                    }
                    foreach (var app in AppList) //Error throws here in log file
                    {
                        bool sappvalue;
                        ApplicationMonitorScript appmonitor = new ApplicationMonitorScript();

                        sappvalue = appmonitor.UrlIsValid(app);
                        if (sappvalue == false)
                        {
                            MessageBox.Show("Stopped");
                        }
                        else
                        {
                             MessageBox.Show("start");
                        }
                    }
                }

}




What I have tried:

List<string> AppList = new List<string>()

  public void loadApplicationDetail()
{
AppList.Clear();
                var Selectlistapp = Dbl.ReturnDataset("select Address,Aort from tblor_Details");
                if (Selectlistapp.Tables[0].Rows.Count > 0)
                {
                    for (var i = 0; i < Selectlistapp.Tables[0].Rows.Count; i++)
                    {
                        string Address = Selectlistapp.Tables[0].Rows[i]["Address"].ToString();
                        string Aort = Selectlistapp.Tables[0].Rows[i]["Aort"].ToString();
                        AppList.Add("http://" + Address + ":" + Aort + "/");
                    }
                    foreach (var app in AppList) //Error throws here in log file
                    {
                        bool sappvalue;
                        ApplicationMonitorScript appmonitor = new ApplicationMonitorScript();

                        sappvalue = appmonitor.UrlIsValid(app);
                        if (sappvalue == false)
                        {
                            MessageBox.Show("Stopped");
                        }
                        else
                        {
                             MessageBox.Show("start");
                        }
                    }
                }

}
Posted
Updated 16-Mar-17 3:33am
Comments
CHill60 16-Mar-17 9:30am    
This usually means that you have deleted or added to the List while in the foreach or for loop. I can't see where you are doing that though.

1 solution

You cannot modify the target of a foreach loop inside the loop: if you do, the error will be thrown when the loop tries to take the next value.
The code you show doesn't directly modify AppList, but it does seem to be a class level variable.
So there are three ways this could be happening:
1) The ApplicationMonitorScript constructor is modifying the list.
2) The ApplicationMonitorScript.UrlIsValid method is modifying the list
3) There is another thread running that modifies the list, or also uses it's enumerators.

Start with the easy ones, and if they don't seem to do anything, start looking at your other threads.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900