假定现在有一个循环,里面开启了10个线程。
循环里面包含如下两行代码:Thred t = new Thread(Run);
t.Start();怎么判断这10个线程都跑完了,其中的资源都释放完毕了?

解决方案 »

  1.   


    再定义一个AutoEvent[10]的数组
    给Run方法的最后加上
    AutoEvent[x].Set();Thred t = new Thread(Run);
    t.Start();在Start的后边加个WaitHandler.All(AutoEvent)
      

  2.   

    我以前是这样做的
    定义一个全局变量
    volatile int count=0;
    在Run方法的最后面加
    count++;
    if(count>=10)
    MessageBox.Show("执行完毕");还有一种是定义一个全局线程数组,再在timer控件里面判断线程状态。
    Thread[] threadRun=new Thread[10];
    for(int i=0;i<10;i++){
      Thred t = new Thread(Run);
      threadRun[i]=t;
      t.Start();
    }
      

  3.   


    那代码应该大致是下面这样了:class Program
        {
            private static AutoResetEvent[] autoEvents = new AutoResetEvent[10];        static void Main(string[] args)
            {
               
                for(int i = 0 ; i < 10; i++)
                {
                    autoEvents[i] = new AutoResetEvent(false);
                    Thread t = new Thread(Run);
                    t.Name = i.ToString();
                    t.Start(t.Name);
                    
                    autoEvents[i].Set();
                }
                WaitHandle.WaitAll(autoEvents);            Console.WriteLine("所有线程都结束完毕");
                Console.ReadKey();
            }        public static void Run(object o)
            {
                Console.WriteLine(string.Format("当前时间{0},当前线程{1}", DateTime.Now, o.ToString()));
            }
        }
      

  4.   


    我测试了一下,貌似结果不争取。测试代码如下:
    class Program
        {
            private static AutoResetEvent[] autoEvents = new AutoResetEvent[10];        static void Main(string[] args)
            {
               
                for(int i = 0 ; i < 10; i++)
                {
                    autoEvents[i] = new AutoResetEvent(false);
                    Thread t = new Thread(Run);
                    t.Name = i.ToString();
                    t.Start(t.Name);
                    
                    autoEvents[i].Set();
                }
                WaitHandle.WaitAll(autoEvents);            Console.WriteLine("所有线程都结束完毕");
                Console.ReadKey();
            }        public static void Run(object o)
            {
                for (int i = 1; i < 5; i++)
                {
                    Console.WriteLine(string.Format("当前线程{0},当前时间{1},当前数字{2}", o.ToString(),DateTime.Now, i));
                }      
            }
        }
    测试结果如图:
      

  5.   


    要改一下,对于线程你要选有参传入,或无参你自己看着吧,都一样 private static AutoResetEvent[] autoEvents = new AutoResetEvent[10];        public static void Run()
            {
                int index = int.Parse(Thread.CurrentThread.Name);
                Console.WriteLine(string.Format("当前时间{0},当前线程{1}", DateTime.Now, index));
                autoEvents[index].Set();  <---这里
            }        static void Main(string[] args)
            {            for (int i = 0; i < 10; i++)
                {
                    autoEvents[i] = new AutoResetEvent(false);
                    Thread t = new Thread(new ThreadStart(Run));
                    t.Name = i.ToString();
                    t.Start();
                }
                WaitHandle.WaitAll(autoEvents); <---这里
                Console.WriteLine("所有线程都结束完毕");
                Console.ReadKey();
    }