foreach (string i in Enum.GetNames(typeof(Citis)))
            {
                string a = i;
                ThreadForCity ins=new ThreadForCity(a);
                Thread t=new Thread(new ThreadStart(ins.ThreadDo));
                t.Start();
             
                            }
主线程中通过判断枚举 Citis从而判断需要开几个线程。
但是我现在希望在全部线程结束后输出一个信号,但是现在不知如何判断全部线程已经结束。
大大们帮忙啊

解决方案 »

  1.   

    定义一个线程数组
    Thread[]
    然后判断每个线程是否isalive
      

  2.   

    IsAlive判断,然后用Join()等着直到完成
      

  3.   

    使用ManualResetEvent进行控制ManualResetEvent e = new ManualResetEvent(false);
    Thread[] threads = new Thread[4];
    int count = threads.Lenght;
    for(int i = 0; i<count; ++i)
    {
       threads[i] = new Thread(delegate()
       {
           //具体操作
           if(Interlocked.Decrement(ref count) == 0)
               e.Set();
       }).Start(); 
    }
    e.WaitOne();
    Console.WriteLine("线程执行结束!");
      

  4.   

    写错了一点
    for(int i = 0; i <count; ++i) 

       threads[i] = new Thread(delegate() 
       { 
           //具体操作 
           if(Interlocked.Decrement(ref count) == 0) 
               e.Set(); 
       });
       threads[i].Start();

      

  5.   


    public void testThreads()
    {
       ManualResetEvent[] _ManualEvents = new ManualResetEvent[10]; 
       for (int i = 0; i < 10; i++)
       {
            _ManualEvents[i] = new ManualResetEvent(false);
            System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(testMethod), _ManualEvents[i]);
       }
       WaitHandle.WaitAll(_ManualEvents);
       //All threads have completed.

    public void testMethod(object objEvent)
    {
         //TODO: Add your code here
         ManualResetEvent e = (ManualResetEvent)objEvent;
         e.Set();
    }