当前位置:编程学习 > C#/ASP.NET >>

高手请进-分析一段代码

这段代码有没有可能崩溃?崩溃的原因是什么?怎么解决?

using System.Collections.Generic;
using System.Threading;
using System;
class Program
{
  static List<string> m_mylist= new List<string> ();
  static bool m_bStarted = false;
  
  static void Main()
  {
    // Start Thread to add 1000 items to the list
    ThreadPool.QueueUserWorkItem((waitCallback)Proc1);

   // Wait till the thread has started
   while(!m_bStarted){}

  // Loop round 20 times
  for (int i=0; i<20;i++)
  {
    // Print out each item in the list
    foreach (sting myString in m_myList)
    {
      Console.WriteLine("Outputing string: {0}", myString);
      Thread.Sleep(100);
    }
  }
  }
  
  static void Proc1(object state)
  {
    Console.WriteLine("Adding items to list");
   
    for (int i=0; i<1000; i++)
    {
      m_myList.Add("hello"+i.ToString());
      m_bStarted=true;
      Thread.Sleep(100);
    }
  }
} --------------------编程问答--------------------

foreach (sting myString in m_myList)
{
  Console.WriteLine("Outputing string: {0}", myString);
  Thread.Sleep(100);
}


==>


for (int j = 0; j < m_mylist.Count; j++)
{
  Console.WriteLine("Outputing string: {0}", mylist[j]);
  Thread.Sleep(100);
}


foreach循环过程中的List集合的大小是不能改变的 --------------------编程问答-------------------- 运行一下就知道了 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,