如何判断线程已经结束 假定现在有一个循环,里面开启了10个线程。循环里面包含如下两行代码:Thred t = new Thread(Run);t.Start();怎么判断这10个线程都跑完了,其中的资源都释放完毕了? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 再定义一个AutoEvent[10]的数组给Run方法的最后加上AutoEvent[x].Set();Thred t = new Thread(Run);t.Start();在Start的后边加个WaitHandler.All(AutoEvent) 我以前是这样做的定义一个全局变量volatile int count=0;在Run方法的最后面加count++;if(count>=10)MessageBox.Show("执行完毕");还有一种是定义一个全局线程数组,再在timer控件里面判断线程状态。Thread[] threadRun=new Thread[10];for(int i=0;i<10;i++){ Thred t = new Thread(Run); threadRun[i]=t; t.Start();} 那代码应该大致是下面这样了:class Program { private static AutoResetEvent[] autoEvents = new AutoResetEvent[10]; static void Main(string[] args) { for(int i = 0 ; i < 10; i++) { autoEvents[i] = new AutoResetEvent(false); Thread t = new Thread(Run); t.Name = i.ToString(); t.Start(t.Name); autoEvents[i].Set(); } WaitHandle.WaitAll(autoEvents); Console.WriteLine("所有线程都结束完毕"); Console.ReadKey(); } public static void Run(object o) { Console.WriteLine(string.Format("当前时间{0},当前线程{1}", DateTime.Now, o.ToString())); } } 我测试了一下,貌似结果不争取。测试代码如下:class Program { private static AutoResetEvent[] autoEvents = new AutoResetEvent[10]; static void Main(string[] args) { for(int i = 0 ; i < 10; i++) { autoEvents[i] = new AutoResetEvent(false); Thread t = new Thread(Run); t.Name = i.ToString(); t.Start(t.Name); autoEvents[i].Set(); } WaitHandle.WaitAll(autoEvents); Console.WriteLine("所有线程都结束完毕"); Console.ReadKey(); } public static void Run(object o) { for (int i = 1; i < 5; i++) { Console.WriteLine(string.Format("当前线程{0},当前时间{1},当前数字{2}", o.ToString(),DateTime.Now, i)); } } }测试结果如图: 要改一下,对于线程你要选有参传入,或无参你自己看着吧,都一样 private static AutoResetEvent[] autoEvents = new AutoResetEvent[10]; public static void Run() { int index = int.Parse(Thread.CurrentThread.Name); Console.WriteLine(string.Format("当前时间{0},当前线程{1}", DateTime.Now, index)); autoEvents[index].Set(); <---这里 } static void Main(string[] args) { for (int i = 0; i < 10; i++) { autoEvents[i] = new AutoResetEvent(false); Thread t = new Thread(new ThreadStart(Run)); t.Name = i.ToString(); t.Start(); } WaitHandle.WaitAll(autoEvents); <---这里 Console.WriteLine("所有线程都结束完毕"); Console.ReadKey();} C# 通过一个程序设置另一个程序活动窗体具有焦点控件的文本 两张数据表同时显示在datagridview控件中 C# 高斯列主元消去法 MRP计算方法优化【请高手帮忙】 多个复选框查询 请教WF问题在线等 ~~~~~ web.config详解??急~~ C#高手们请进啊!!急!! 对C# winform应用程序进行二次修改的问题。 添加这两个控件,解释一下 c#窗体程序调用oracle存储过程返回ref cursor 求解devexpress控件中的spinEdit控件用法
再定义一个AutoEvent[10]的数组
给Run方法的最后加上
AutoEvent[x].Set();Thred t = new Thread(Run);
t.Start();在Start的后边加个WaitHandler.All(AutoEvent)
定义一个全局变量
volatile int count=0;
在Run方法的最后面加
count++;
if(count>=10)
MessageBox.Show("执行完毕");还有一种是定义一个全局线程数组,再在timer控件里面判断线程状态。
Thread[] threadRun=new Thread[10];
for(int i=0;i<10;i++){
Thred t = new Thread(Run);
threadRun[i]=t;
t.Start();
}
那代码应该大致是下面这样了:class Program
{
private static AutoResetEvent[] autoEvents = new AutoResetEvent[10]; static void Main(string[] args)
{
for(int i = 0 ; i < 10; i++)
{
autoEvents[i] = new AutoResetEvent(false);
Thread t = new Thread(Run);
t.Name = i.ToString();
t.Start(t.Name);
autoEvents[i].Set();
}
WaitHandle.WaitAll(autoEvents); Console.WriteLine("所有线程都结束完毕");
Console.ReadKey();
} public static void Run(object o)
{
Console.WriteLine(string.Format("当前时间{0},当前线程{1}", DateTime.Now, o.ToString()));
}
}
我测试了一下,貌似结果不争取。测试代码如下:
class Program
{
private static AutoResetEvent[] autoEvents = new AutoResetEvent[10]; static void Main(string[] args)
{
for(int i = 0 ; i < 10; i++)
{
autoEvents[i] = new AutoResetEvent(false);
Thread t = new Thread(Run);
t.Name = i.ToString();
t.Start(t.Name);
autoEvents[i].Set();
}
WaitHandle.WaitAll(autoEvents); Console.WriteLine("所有线程都结束完毕");
Console.ReadKey();
} public static void Run(object o)
{
for (int i = 1; i < 5; i++)
{
Console.WriteLine(string.Format("当前线程{0},当前时间{1},当前数字{2}", o.ToString(),DateTime.Now, i));
}
}
}
测试结果如图:
要改一下,对于线程你要选有参传入,或无参你自己看着吧,都一样 private static AutoResetEvent[] autoEvents = new AutoResetEvent[10]; public static void Run()
{
int index = int.Parse(Thread.CurrentThread.Name);
Console.WriteLine(string.Format("当前时间{0},当前线程{1}", DateTime.Now, index));
autoEvents[index].Set(); <---这里
} static void Main(string[] args)
{ for (int i = 0; i < 10; i++)
{
autoEvents[i] = new AutoResetEvent(false);
Thread t = new Thread(new ThreadStart(Run));
t.Name = i.ToString();
t.Start();
}
WaitHandle.WaitAll(autoEvents); <---这里
Console.WriteLine("所有线程都结束完毕");
Console.ReadKey();
}