简体   繁体   English

尝试修改字典时出现异常

[英]Exception when attempting to modify Dictionary

I was creating a Dictionary to manage Threads and I ran into this exception when I attempt to clear dead threads after adding some Threads into the dictionary.我正在创建一个字典来管理线程,当我在将一些线程添加到字典中后尝试清除死线程时遇到了这个异常。

using System;
using System.Collections.Generic;
using System.Threading;

namespace SysCat {
    public class ThreadManager {
        public static readonly ThreadManager GlobalThreadManager = new ThreadManager( );

        /// <summary>
        /// Initializes a new instance of the <see cref="SysCat.ThreadManager"/> class.
        /// </summary>
        public ThreadManager( ) {

        }

        /// <summary>
        /// Create a new ThreadManager with all the Threads within baseTM and deinitializes baseTM
        /// </summary>
        public ThreadManager( ThreadManager baseTM ) {
            this.threads = baseTM.threads;
            baseTM.threads.Clear( );
        }

        private Dictionary<Guid,Thread> threads = new Dictionary<Guid, Thread>( );

        /// <summary>
        /// Attempts to obtain an unused ThreadID within an attempt limit
        /// </summary>
        /// <returns>The unused ThreadID, if found</returns>
        /// <param name="attempts">The number of iterations to try.</param>
        public Guid getUnusedThreadID( int attempts ) {
            lock( threads ) {
                Guid threadID;
                int totalAttempts = attempts;
                while( threads.ContainsKey( ( threadID = Guid.NewGuid( ) ) ) ) {
                    attempts--;
                    if( attempts == 0 )
                        throw new NoOpenThreadIDException( totalAttempts );
                }
                return threadID;
            }
        }

        /// <summary>
        /// Attempts to get a Thread via its ThreadID, if it exists
        /// </summary>
        /// <returns>The Thread</returns>
        /// <param name="threadID">The ThreadID to use</param>
        public Thread getThread( Guid threadID ) {
            lock( threads ) {
                Thread tryGet;
                if( !threads.TryGetValue( threadID, out tryGet ) )
                    throw new ArgumentException( "Specified ThreadID does not exist in this ThreadManager" );
                return tryGet;
            }
        }

        /// <summary>
        /// Attempts to get a ThreadID via the Thread
        /// </summary>
        /// <returns>The ThreadID</returns>
        /// <param name="thread">The Thread to use</param>
        public Guid getThreadID( Thread thread ) {
            lock( threads ) {
                if( threads.ContainsValue( thread ) ) {
                    foreach( Guid id in threads.Keys ) {
                        Thread t;
                        threads.TryGetValue( id, out t );
                        if( t.Equals( thread ) )
                            return id;
                    }
                    // I should never get here
                    return Guid.Empty;
                }
                else
                    throw new ArgumentException( "Specified Thread does not exist in this ThreadManager" );
            }
        }

        /// <summary>
        /// Adds the Thread, unless it cannot find an open ThreadID
        /// </summary>
        /// <returns>The ThreadID used to register the Thread</returns>
        /// <param name="thread">The Thread to register</param>
        public Guid addThread( Thread thread ) {
            lock( threads ) {
                Guid id;
                try {
                    id = getUnusedThreadID( 500 );
                }
                catch( NoOpenThreadIDException e ) {
                    throw e;
                }
                threads.Add( id, thread );
                return id;
            }
        }

        /// <summary>
        /// Attempts to clear all stopped and null Threads
        /// </summary>
        /// <returns>The number of dead Threads cleared</returns>
        public int clearDeadThreads() {
            int cleared = 0;
            lock(threads) {
                foreach( Guid id in threads.Keys ) {
                    Thread thread;
                    threads.TryGetValue( id, out thread );
                    if( thread == null ) {
                        // Does not exist, Thread is dead
                        cleared++;
                        threads.Remove( id );
                        continue;
                    }
                    if( !thread.IsAlive ) {
                        // Not alive, Thread is dead
                        cleared++;
                        threads.Remove( id );
                        continue;
                    }
                }
            }

            return cleared;
        }

        public class NoOpenThreadIDException : Exception {
            public NoOpenThreadIDException( int attempts )
                : base( string.Format( "Unable to find an open ThreadID within the attempt limit of {0}", attempts ) ) {
            }
        }
    }
}

And I am getting this exception: System.InvalidOperationException with the message: Collection was modified;我收到此异常:System.InvalidOperationException 消息:集合已修改; enumeration operation may not execute枚举操作可能不会执行

I am unsure why this is happening, any help would be appreciated.我不确定为什么会发生这种情况,任何帮助将不胜感激。

This is the code I used to test it:这是我用来测试它的代码:

using System;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Threading;

using _TM = SysCat.ThreadManager;

namespace SysCat {
    public class SysCat {
        Thread T = new Thread( new ThreadStart( delegate {
            while(true) Thread.Sleep(250);
        } ) );
        Thread T2 = new Thread( new ThreadStart( delegate {

        } ) );
        Thread T3 = new Thread( new ThreadStart( delegate {

        } ) );
        _TM.GlobalThreadManager.addThread( T );
        _TM.GlobalThreadManager.addThread( T2 );
        _TM.GlobalThreadManager.addThread( T3 );
            Console.WriteLine( _TM.GlobalThreadManager.clearDeadThreads( ) );
            Console.ReadLine( );
        }
    }
}

you can not modify an IEnumerable while you are enumerating over it.在枚举时不能修改IEnumerable

you can do a simple hack and get a copy of what you are enumerating so changes in it does not result in System.InvalidOperationException .你可以做一个简单的 hack 并获取你正在枚举的内容的副本,因此它的更改不会导致System.InvalidOperationException

for example instead of例如,而不是

foreach( Guid id in threads.Keys) 

use

foreach( Guid id in threads.Keys.ToList()) 

keep in mind that you can use data structures that supports concurrency请记住,您可以使用支持并发的数据结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM