.
static void Main(string[] args) { Console.WriteLine("Starting program..."); Thread t = new Thread(PrintNumbersWithStatus); Thread t2 = new Thread(DoNothing); Console.WriteLine(t.ThreadState.ToString()); t2.Start(); t.Start(); for (int i = 1; i < 30; i++) { Console.WriteLine("主线程 : " + t.ThreadState.ToString()+" "+i +";"); } Thread.Sleep(TimeSpan.FromSeconds(6)); //----------------------------------- t.Abort();//阻碍主线程继续运行,等待t线程运行完成。 //在调用此方法的线程上引发 System.Threading.ThreadAbortException, //以开始终止此线程的过程。调用此方法通常会终止线程。 Console.WriteLine("A thread has been aborted"); Console.WriteLine("t线程状态:"+ t.ThreadState.ToString());//AbortRequested 线程的Thread.Abort()方法已被调用,但是线程还未停止; Console.WriteLine("t2线程状态:" + t2.ThreadState.ToString());//Stopped 线程已经被停止; Console.ReadKey(); } static void DoNothing() { for (int i = 1; i < 10; i++) { Thread.Sleep(TimeSpan.FromSeconds(2)); Console.WriteLine("t2:" + i); } } static void PrintNumbersWithStatus() { for (int i = 1; i < 10; i++) { Console.WriteLine("t : " + i); Thread.Sleep(TimeSpan.FromSeconds(2)); } }
、、、