这几天在网上找了个天翻地覆
依然没有搞定因为没有用线程池
所以 WaitHandle 是用不了了Thread.Join() 也试了一下
程序跑的时候会处于假死静态小弟新手
折腾C#才几天
希望各位前辈能赏个详细点的例子
跪谢了
public   Form1_Load() 

      for(int   i=0;i <10;i++)        //创建线程
      { 
              Class1   MyClass   =   new   Class1(); 
              Thread   t   =   new   Thread(MyClass.MyMethod); 
              t.Start(); 
      } 
      //   我这里想在10个线程都结束后显示这段话   要怎么写?多谢! 
      MessageBox.Show( "All   Thread   Finished! "); 
}

解决方案 »

  1.   

    有一个不用「翻」的招:
    用10个公用变量v1-v10,在每个线程最后一个吧对应的v设为true,并判断所有v1&v2&...&v10==true,如果是true,则可以显示了。。如果不想用这笨招。。你还是继续「翻」吧。。
      

  2.   

    参考:How to wait for thread to finish with .NET?
    http://stackoverflow.com/questions/1584062/how-to-wait-for-thread-to-finish-with-net文中介绍了这几种方式:
    1. Thread.Join
    2. Use a WaitHandle
    3. Fire an event
    4. Use a delegate
    5. Do it asynchronously instead
      

  3.   

    你不用多线程,直接在循环中调用MyClass.MyMethod不就行了,这里用多线程的用意何在?
      

  4.   

    public   Form1_Load() 

          for(int   i=0;i <10;i++)        //创建线程
          { 
                  Class1   MyClass   =   new   Class1(); 
                  Thread   t   =   new   Thread(MyClass.MyMethod); 
                  t.Start(); 
          } 
          //   我这里想在10个线程都结束后显示这段话   要怎么写?多谢! 
          MessageBox.Show( "All   Thread   Finished! "); 
    }
    Thread有的个属性IsAlive可以判断线程的状态,但是你的线程最好有变量名这样可以遍历再判断线程状态
      

  5.   

    public   Form1_Load() 

          for(int   i=0;i <10;i++)        //创建线程
          { 
                  Class1   MyClass   =   new   Class1(); 
                  Thread   t   =   new   Thread(MyClass.MyMethod); 
                  t.Start(); 
          } 
          //   我这里想在10个线程都结束后显示这段话   要怎么写?多谢! 
          MessageBox.Show( "All   Thread   Finished! "); 
    }
    Thread有的个属性IsAlive可以判断线程的状态,但是你的线程最好有变量名这样可以遍历再判断线程状态
      

  6.   

    我是这样做判断的,定义一个全局变量
    virtual int runThreadCount=0;
    在MyClass.MyMethod方法的最后面添加如下代码
    runThreadCount++;
    if(runThreadCount>=10)
       MessageBox.Show("执行完毕!");
    --------------------------------------------------
    for(int   i=0;i <10;i++)        //创建线程
          { 
                  Class1   MyClass   =   new   Class1(); 
                  Thread   t   =   new   Thread(MyClass.MyMethod); 
                  t.Start(); 
          } 
          这里是无法判断的
          MessageBox.Show( "All   Thread   Finished! "); 
      

  7.   

                   System.Threading.Thread t = new System.Threading.Thread();
                    t.IsAlive 
      

  8.   

    给你代码:
    这是个思路 当然 可能你的项目不一样,但是改下就OK了using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;using System.Net;
    using System.IO;
    using Test.Code;
    using System.Threading;
    namespace Test
    {
        class Program
        {
            public static int Flag = 10;
            public static bool FinishFlag = false;
            public static object objLock = new object();
            static void Main(string[] args)
            {
        
               
                TestEventClass tec = new TestEventClass();
                tec.TestEvent += new TestEventClass.TestEventHandler(tec_TestEvent);
             
                Thread[] threadarray = new Thread[10];
                for (int i = 0; i < 10; i++)
                {
                    threadarray[i] = new Thread(new ParameterizedThreadStart(tec.RaiseEvent));
                   
                    threadarray[i].Start(i);
                    
                }
                while (FinishFlag == false)
                {
                    Thread.Sleep(100);
                    
                }            Console.WriteLine("线程都执行完毕了");            Console.ReadLine();
               
            }        static void tec_TestEvent(object sender, TestEventClass.TestEventArgs e)
            {
                /*
                 做你要做的事
                 */
                
                lock (objLock)
                {
                    Flag += 1;
                    if (Flag >= 10)
                    {
                        FinishFlag = true;
                    }
                }        }      
          
        }    public class TestEventClass
        {
            //定义事件参数类
            public class TestEventArgs : EventArgs
            {
                public readonly object KeyToRaiseEvent;
                public TestEventArgs(object keyToRaiseEvent)
                {
                    KeyToRaiseEvent = keyToRaiseEvent;
                }
            }        //定义delegate
            public delegate void TestEventHandler(object sender, TestEventArgs e);
            //用event 关键字声明事件对象
            public event TestEventHandler TestEvent;        //事件触发方法
            protected virtual void OnTestEvent(TestEventArgs e)
            {
                if (TestEvent != null)
                    TestEvent(this, e);
            }        //引发事件
            public void RaiseEvent(object obj)
            {
                TestEventArgs e = new TestEventArgs(obj);
                OnTestEvent(e);
            }
        } 
    }
      

  9.   

    忘记说了,我那个是要参数测试的,你改下就OK了,改成不要参数的。。爬虫的话 url放在队列列吧,队列空了,就说完事了当然要线程安全哦。
      

  10.   


    他那人就那样,喜欢装高深回复别人的帖子,很少正常回复的,都是讲大道理那么牛逼还混什么CSDN,这里不适合他,深山老林最适合他,高人都是不入世的嘛
      

  11.   

    如果你的线程定义的是一个类的成员可以通过ISAlive来判断下.isAlive是用来判断线程是否还是活动的。如果用ThreadState来判断的话,得好好查查MSDN哪些状态可用,否则,在主线程中通过ThreadState判断子线程的状态,会出异常(不报错),但是获取不到想要的结果,具体为什么查查MSDN,有一段时间不用线程了,ThreadState中的有点数不清了。
      

  12.   

    试试task吧?//任务集合
                List<Task> ts = new List<Task>();            //干活
                Action<object> action = (object num) => Thread.Sleep((int)num * 1000);            //干完活
                Action<Task> action1 = (Task t) => Console.WriteLine(t.Id + " finish!");            //所有的活干完了
                Action<Task[]> action3 = (Task[] tarr) => Console.WriteLine("all finish!");            //初始化任务,将任务添加进任务集合
                for (int i = 1; i < 5; i++)
                {
                    Task t1 = new Task(action, i);
                    t1.ContinueWith(action1);
                    ts.Add(t1);
                }            //定义任务工厂
                TaskFactory tf = new TaskFactory();
                tf.ContinueWhenAll(ts.ToArray(), action3, CancellationToken.None);            //所有任务开始干活
                ts.ForEach(t =>
                {
                    t.Start();
                    Console.WriteLine(t.Id + " start!");
                });
      

  13.   

    我之前也遇到这个问题,我是做网络代理测试的,用到多线程异步测试提高效率。待所有测试IP都测试完成后,应该给个提示全部测试完成,然后界面上按钮状态等啥的变成可用。
    我目前是这样做的
    1 定义一个类
    muiltTimes()
    {
       public timeCnt;
       public TotalCnt;
       public Action doAfterAllComplete;
    }
    2 将该类的对象传递给 MyClass.MyMethod方法
      在MyClass.MyMethod 方法中对timeCnt计数递增(一定要先锁定对象lock(obj))
      然后判断 timeCnt 是否等于totalCnt如果等于,那么执行委托doAfterAllComplete 
      (委托 doAfterAllComplete ,以及 timeCnt TotalCnt 是在你启用多线程之前初始化的)3 对于线程传递参数,想必应该清楚如果不清楚百度一下...