Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- private static List<string> _collection = new List<string>();
- static void Main(string[] args)
- {
- ThreadPool.QueueUserWorkItem(new WaitCallback(AddItems), null);
- System.Threading.Thread.Sleep(5000);
- ThreadPool.QueueUserWorkItem(new WaitCallback(DisplayItems), null);
- Console.ReadLine();
- }
- public static void AddItems(object state_)
- {
- for (int i = 1; i <= 50; i++)
- {
- _collection.Add(i.ToString());
- Console.WriteLine("Adding " + i);
- System.Threading.Thread.Sleep(150);
- }
- }
- public static void DisplayItems(object state_)
- {
- // This will not throw an exception
- //for (int i = 0; i < _collection.Count; i++)
- //{
- // Console.WriteLine("Reading " + _collection[i]);
- // System.Threading.Thread.Sleep(150);
- //}
- // This will throw an exception
- List<string>.Enumerator enumerator = _collection.GetEnumerator();
- while (enumerator.MoveNext())
- {
- string value = enumerator.Current;
- System.Threading.Thread.Sleep(150);
- Console.WriteLine("Reading " + value);
- }
- }
- }
- public static void AddItems(object state_)
- {
- for (int i = 1; i <= 50; i++)
- {
- _collection.Add(i.ToString());
- Console.WriteLine("Adding " + i);
- }
- }
Add Comment
Please, Sign In to add comment