日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#多線程之線程中止Abort()方法_C#教程

作者:農碼一生 ? 更新時間: 2022-06-18 編程語言

一、簡介

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

欄目分類
最近更新