日期:2014-05-18 浏览次数:20916 次
namespace THreadTes2 { public class Alpha { public void Beta() { while (true) { Console.WriteLine("Alpha.Beta is running in its own thread."); } } } class Simple { static void Main(string[] args) { Console.WriteLine("Thread Start/Stop/Join Sample"); Alpha oAlpha = new Alpha(); //file://这里创建一个线程,使之执行Alpha类的Beta()方法 Thread oThread = new Thread(new ThreadStart(oAlpha.Beta)); oThread.Start(); //lkf try Start Thread.Sleep(100); oThread.Abort();//杀死线程 看看是否进入死循环 //lkf try End while (!oThread.IsAlive)//当线程死了 就会进入死循环 Thread.Sleep(1); //oThread.Abort(); //oThread.Join();//阻塞 调用他的主线程, Console.WriteLine(); Console.WriteLine("Alpha.Beta has finished"); try { Console.WriteLine("试着重启 Alpha.Beta thread"); oThread.Start(); } catch (ThreadStateException) { Console.Write("线程状态异常,尝试重启 Alpha.Beta. "); Console.WriteLine("预计自中止线程不能被重启 lkf 调用的线程无效."); Console.ReadLine(); } //return 0; } } }