首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

多任务情况下.怎么获取线程状态 ?

2012-01-20 
多任务情况下...............如何获取线程状态 ??有多个任务.具体数量不明..这个由用户来选择.我想依次执

多任务情况下...............如何获取线程状态 ??

有多个任务.具体数量不明..这个由用户来选择.

我想依次执行..


用for循环..


第一个任务启动 多个线程来执行任务....
任务执行完毕后..

在启动 第二个任务 ,在启动多个线程来执行任务....
任务执行完毕后..

在启动 第三个任务 ,在启动多个线程来执行任务....
....



现在我就是任务启动后....线程的状态我不知道...所以我也不知道任务是否已经执行完毕.


我如何才能知道任务的线程组是否都已经执行完毕呢...

[解决办法]
看看 MSDN 里面 WaitHandle.WaitAll 的介绍就明白了
[解决办法]

C# code
using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace ThreadWait{    class StataInfo    {        public int index;        public ManualResetEvent manualEvent;        public StataInfo(int index, ManualResetEvent manualEvent)        {            this.index = index;            this.manualEvent = manualEvent;        }    }    class Program    {        static void Main(string[] args)        {            ManualResetEvent[] manualEvents = new ManualResetEvent[6];            for (int i = 0; i < 6; i++)            {                Thread thread = new Thread(new ParameterizedThreadStart(ThreadWork));                manualEvents[i] = new ManualResetEvent(false);                StataInfo stataInfo = new StataInfo(i,manualEvents[i]);                thread.Start(stataInfo);            }            WaitHandle.WaitAll(manualEvents);            Console.WriteLine("All thread have exit!.");            Console.Read();        }        static void ThreadWork(object stata)        {            for(int i=0;i<10;++i)            {                Thread.Sleep(200);            }            StataInfo stataInfo = (StataInfo)stata;            Console.WriteLine(string.Format("The sub thread exit.",stataInfo.index));            ManualResetEvent manualEvent = (ManualResetEvent)stataInfo.manualEvent;            manualEvent.Set();        }    }} 

热点排行