日期:2014-05-17 浏览次数:20819 次
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! "); }
System.Threading.Thread t = new System.Threading.Thread(); t.IsAlive
------解决方案--------------------
给你代码:
这是个思路 当然 可能你的项目不一样,但是改下就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