c# – 获取集合已被修改;枚举操作可能无法执行.例外

前端之家收集整理的这篇文章主要介绍了c# – 获取集合已被修改;枚举操作可能无法执行.例外前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
收集被修改;枚举操作可能无法执行.例外 @H_404_2@码:

public static string GetValue(List<StateBag> stateBagList,string name)
{
    string retValue = string.Empty;

    if (stateBagList != null)
    {
        foreach (StateBag stateBag in stateBagList)
        {
            if (stateBag.Name.Equals(name,StringComparison.InvariantCultureIgnoreCase))
            {
                retValue = stateBag.Value;
            }
        }
    }

    return retValue;
}
@H_404_2@在某个时间点获得此异常并非每次都在这个地方.

@H_404_2@堆栈跟踪:

@H_404_2@at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource
resource)

@H_404_2@at System.Collections.Generic.List`1.Enumerator.MoveNextRare()

@H_404_2@at System.Collections.Generic.List`1.Enumerator.MoveNext()

@H_404_2@at Tavisca.TravelNxt.Shared.Entities.StateBag.GetValue(List`1 stateBagList,String name)

@H_404_2@@no我试过以下代码,但仍然有异常

@H_404_2@码:

class StateBag
{
    public string Name;
    public string Value;
}

class Program
{
    static List<StateBag> _concurrent = new List<StateBag>();

    static void Main()
    {
        var sw = new Stopwatch();
        try
        {
            sw.Start();
            Thread thread1 = new Thread(new ThreadStart(A));
            Thread thread2 = new Thread(new ThreadStart(B));
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();
            sw.Stop();
        }
        catch (Exception ex)
        {
        }


        Console.WriteLine("Average: {0}",sw.ElapsedTicks);
        Console.ReadKey();
    }

    private static Object thisLock = new Object();

    public static string GetValue(List<StateBag> stateBagList,string name)
    {
        string retValue = string.Empty;


        if (stateBagList != null)
        {
            lock (thisLock)
            {
                foreach (StateBag stateBag in stateBagList)
                {
                    if (stateBag.Name.Equals(name,StringComparison.InvariantCultureIgnoreCase))
                    {
                        retValue = stateBag.Value;
                    }
                }
            }

        }



        return retValue;
    }

    static void A()
    {
        for (int i = 0; i < 5000; i++)
        {
            _concurrent.Add(new StateBag() { Name = "name" + i,Value = i.ToString() });
        }
    }

    static void B()
    {
        for (int i = 0; i < 5000; i++)
        {
            var t = GetValue(_concurrent,"name" + i);
        }
    }
}

解决方法

@H_404_2@Getting Collection was modified; enumeration operation may not execute. exception

@H_404_2@原因:当您循环的枚举在同一个线程或其他某个线程中被修改时,会发生此异常.

@H_404_2@现在,在您提供的代码中,没有任何此类方案.这意味着您可能在多线程环境中调用它,并且在其他一些线程中修改了集合.

@H_404_2@解决方案:在枚举上实现锁定,以便一次只能访问一个线程.这样的事情应该做到这一点.

private static Object thisLock = new Object();
public static string GetValue(List<StateBag> stateBagList,string name)
{
    string retValue = string.Empty;

    if (stateBagList != null)
    {
        lock(thisLock)
        {
            foreach (StateBag stateBag in stateBagList)
            {
               if (stateBag.Name.Equals(name,StringComparison.InvariantCultureIgnoreCase))
               {
                retValue = stateBag.Value;
                }
             }
        }
    }

    return retValue;
}

猜你在找的C#相关文章