System.invalidoperationexception:集合已被修改;枚举操作可能无法执行。在system.throwhelper.throwinvalidoperationexception(exceptionresource resource) [英] System.invalidoperationexception: collection was modified; enumeration operation may not execute. At system.throwhelper.throwinvalidoperationexception(exceptionresource resource)

查看:285
本文介绍了System.invalidoperationexception:集合已被修改;枚举操作可能无法执行。在system.throwhelper.throwinvalidoperationexception(exceptionresource resource)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误,我无法解决这个问题,请帮我解决这个问题。

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()







<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");
                        }
                    }
                }

}

推荐答案

你不能在循环中修改 foreach 循环的目标:如果这样做,当循环尝试获取下一个值时将抛出错误。

您显示的代码不会直接修改AppList,但它似乎是一个类级变量。

所以有三种方法可以发生这种情况:

1 )ApplicationMonitorScript构造函数正在修改列表。

2)ApplicationMonitorScript.UrlIsValid方法正在修改列表

3)还有另一个运行的线程修改了列表,或者使用它的枚举器。



从简单的开始,如果他们似乎什么都不做,就开始查看你的其他线程。
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.


这篇关于System.invalidoperationexception:集合已被修改;枚举操作可能无法执行。在system.throwhelper.throwinvalidoperationexception(exceptionresource resource)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆