日期:2014-05-17  浏览次数:20804 次

苦恼几天了,请问如何判断线程结束
这几天在网上找了个天翻地覆
依然没有搞定

因为没有用线程池
所以 WaitHandle 是用不了了

Thread.Join() 也试了一下
程序跑的时候会处于假死静态

小弟新手
折腾C#才几天
希望各位前辈能赏个详细点的例子
跪谢了

C# code

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! "); 
}



------解决方案--------------------
参考:

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
------解决方案--------------------
我是这样做判断的,定义一个全局变量
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! ");
------解决方案--------------------
C# code
System.Threading.Thread t = new System.Threading.Thread();
                t.IsAlive

------解决方案--------------------
给你代码:
这是个思路 当然 可能你的项目不一样,但是改下就OK了

C# code
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