/
Thread t ;
Thread t2;
t.Abort()执行后,会阻止主线程继续运行,但是不会影响t2线程的执行。
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("Thread t : " + t.ThreadState.ToString()); } Thread.Sleep(TimeSpan.FromSeconds(6)); //----------------------------------- t.Abort();//阻碍主线程继续运行,等待t线程运行完成。 //在调用此方法的线程上引发 System.Threading.ThreadAbortException, //以开始终止此线程的过程。调用此方法通常会终止线程。 Console.WriteLine("A thread has been aborted"); Console.WriteLine(t.ThreadState.ToString());//AbortRequested 线程的Thread.Abort()方法已被调用,但是线程还未停止; Console.WriteLine(t2.ThreadState.ToString());//Stopped 线程已经被停止; Console.ReadKey(); } static void DoNothing() { for (int i = 1; i < 10; i++) { Thread.Sleep(TimeSpan.FromSeconds(2)); Console.WriteLine("xxxxxxxxxxx"+i); } } static void PrintNumbersWithStatus() { for (int i = 1; i < 10; i++) { Console.WriteLine("t : " + i); Thread.Sleep(TimeSpan.FromSeconds(2)); } }
https://www.cnblogs.com/wyt007/p/9486752.html
、、、