我的程序共开启了5个线程,其中有四个线程是访问同一个对象
另外一个线程是访问其他对象,请问我如何判断这几个线程已经全部执行完?
由于程序中涉及到其他操作,故不使用线程池,希望大家帮帮忙啊,谢谢了!

解决方案 »

  1.   

    多线程里不是有个join方法吗
    具体用法忘记了,不过这个好像是可以的
      

  2.   

    using System;
    using System.Threading;class IsThreadPool
    {
        static void Main()
        {
            AutoResetEvent autoEvent = new AutoResetEvent(false);        Thread regularThread = 
                new Thread(new ThreadStart(ThreadMethod));
            regularThread.Start();
            ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), 
                autoEvent);        // Wait for foreground thread to end.
            regularThread.Join();        // Wait for background thread to end.
            autoEvent.WaitOne();
        }    static void ThreadMethod()
        {
            Console.WriteLine("ThreadOne, executing ThreadMethod, " +
                "is {0}from the thread pool.", 
                Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
        }    static void WorkMethod(object stateInfo)
        {
            Console.WriteLine("ThreadTwo, executing WorkMethod, " +
                "is {0}from the thread pool.", 
                Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");        // Signal that this thread is finished.
            ((AutoResetEvent)stateInfo).Set();
        }
    }
      

  3.   

    给每个子线程调用join方法
    这样,主线程会等到全部的子线程结束后才继续执行
      

  4.   

    using System;
    using System.Threading;class IsThreadPool
    {
        static void Main()
        {
            AutoResetEvent autoEvent = new AutoResetEvent(false);        Thread regularThread = new Thread(new ThreadStart(ThreadMethod));
            regularThread.Start();        ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod),autoEvent);        regularThread.Join();
            autoEvent.WaitOne();
        }    static void ThreadMethod()
        {
            Console.WriteLine("ThreadOne, executing ThreadMethod, " + "is {0}from the thread pool.",
                Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
        }    static void WorkMethod(object stateInfo)
        {
            Console.WriteLine("ThreadTwo, executing WorkMethod, " + "is {0}from the thread pool.",
                Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");        // Signal that this thread is finished. 
            ((AutoResetEvent)stateInfo).Set();
        }
    }
      

  5.   

    Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess.Threads.Count.ToString());
      

  6.   

    int count=0;
    for(int i=0;i<5;i++)
    {
    Thread th=new Thread(
    ()=>{
    线程体;
    if(++count==5)...});
    th.Start();
    }
      

  7.   

    我使用的方法:
    1.保存创建线程时返回的线程句柄。
    2.在线程结束之前(线程return之前的一条语句),向主窗口发送一条线程结束的消息,将线程的标识通过wParam参数传递过去。主窗口在处理这条消息时,将相应线程句柄变量关闭(CloseHandle),并将相应变量值为NULL。
    3.在需要判断线程是否结束的地方,只需要判断相应线程句柄变量是否为空即可
      

  8.   

    System.Threading.ManualEvent manuls[] = new System.Threading.ManualEvent[5];....每个子线程加一个.
    manuls[i].Reset();//线程起始
    ..子线程代码
    manual[i].Set();//线程结束
    主线程添加如下:
    manuls.WaitAll();...线程结束后的代码.
      

  9.   


    弄个Bool数组,里面放5个false,当每个线程结束的时候将数组里的一个bool值写成true
    主线程循环判断这个数组里的5个值是否都是true了,是就是都执行完了,不是就等待。
      

  10.   

    如果只需要简单的知道有几个线程执行完成了,不需要知道具体是哪个线程,可以用这种简单的计数器的方式。
    每个线程执行完成时,使用 Interlocked.Increment (Int32) 计数。
    推荐采用20楼的方法。
      

  11.   

    using System;
    using System.Threading;class IsThreadPool
    {
        static void Main()
        {
            AutoResetEvent autoEvent = new AutoResetEvent(false);        Thread regularThread = new Thread(new ThreadStart(ThreadMethod));
            regularThread.Start();        ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod),autoEvent);        regularThread.Join();
            autoEvent.WaitOne();
        }    static void ThreadMethod()
        {
            Console.WriteLine("ThreadOne, executing ThreadMethod, " + "is {0}from the thread pool.",
                Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
        }    static void WorkMethod(object stateInfo)
        {
            Console.WriteLine("ThreadTwo, executing WorkMethod, " + "is {0}from the thread pool.",
                Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");        // Signal that this thread is finished. 
            ((AutoResetEvent)stateInfo).Set();
        }
    }
      

  12.   

    在需要等待的代码之前写
    foreach(Thread thread in threads)
    {
    thread.Join();
    }
      

  13.   

    写一段吧,简单点的.void MianThread()
            {
                ManualResetEvent[] manuls = new ManualResetEvent[5];
                for (int i = 0;i < manuls.Length;i++)
                {
                    manuls[i] = new ManualResetEvent(false);
                    Thread thread = new Thread(new ParameterizedThreadStart(ChildThread));
                    thread.Start(manuls[i]);
                }
                WaitHandle.WaitAll(manuls);
                //主过程的后续工作
            }
            void ChildThread(object obj)
            {
                ManualResetEvent muanl = obj as ManualResetEvent;
                muanl.Reset();
                //子过程            muanl.Set();
            }
      

  14.   

    学习了,MARK
    ASP.NET技术开发平台:http://www.singletowm.com