多線程Abort()
一、簡介:
Abort()方法用來終止線程,調用此方法強制停止正在執行的線程,它會抛出一個ThreadAbortException異常進而導緻目标線程的終止。
二、代碼
class Program
{
static void Main(string[] args)
{
Thread thread = new Thread(ThreadMethod); //執行的必須是無傳回值的方法
thread.Name = "子線程A";
thread.Start();
Console.ReadKey();
}
public static void ThreadMethod(object parameter)
{
Console.WriteLine("我是:{0},我要終止了!", Thread.CurrentThread.Name);
//開始終止線程
Thread.CurrentThread.Abort();
//下面的代碼不會執行
for (int i = 0; i < 10; i++)
{
Console.WriteLine("我是:{0},我循環{1}次", Thread.CurrentThread.Name, i);
}
}
}
三、運作結果:

本文來自部落格園,作者:農碼一生,轉載請注明原文連結:https://www.cnblogs.com/wml-it/p/14821114.html
技術的發展日新月異,随着時間推移,無法保證本部落格所有内容的正确性。如有誤導,請大家見諒,歡迎評論區指正! 個人開源代碼連結: GitHub:https://github.com/ITMingliang
Gitee:https://gitee.com/mingliang_it
GitLab:https://gitlab.com/ITMingliang
進開發學習交流群: