C#线程Abort中止
方法的作用是:终止线程。如果未完成中止操作,则引发ThreadAbortException。
using System; using System.Threading; public class MyThread { public void Thread1() { for (int i = 0; i < 10; i++) { Console.WriteLine(i); Thread.Sleep(200); } } } public class ThreadExample { public static void Main() { Console.WriteLine("Start of Main"); MyThread mt = new MyThread(); Thread t1 = new Thread(new ThreadStart(mt.Thread1)); Thread t2 = new Thread(new ThreadStart(mt.Thread1)); t1.Start(); t2.Start(); try { t1.Abort(); t2.Abort(); } catch (ThreadAbortException tae) { Console.WriteLine(tae.ToString()); } Console.WriteLine("End of Main"); } }
输出:
Start of Main 0 End of Main
祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)
精选教程推荐
👇 以下精选教程可能对您有帮助,拓展您的技术视野
暂无学习笔记,成为第一个分享的人吧!
您的笔记将帮助成千上万的学习者