public static void CallToChildThread()
        {
            try
            {
            Console.WriteLine("执行子程序");
            int sleepfor = 5000;
            Thread.Sleep(sleepfor);
            Console.WriteLine($"暂停{sleepfor/1000}秒");
            }catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            //finally { Console.WriteLine("无法捕获线程异常"); }

        }

 

用Tread创出出线程

        static void Main(string[] args)
        {
            ThreadStart threadStart =new ThreadStart(CallToChildThread);
            Console.WriteLine("asd");
            Thread thread = new Thread(threadStart);
            Console.WriteLine("asdfg");
            thread.Start();//开启线程
            Thread.Sleep(1000);//延迟线程
            Console.WriteLine("1234");
            Console.ReadLine();

        }

先执行

            Thread.Sleep(1000);//延迟线程
            Console.WriteLine("1234");
后执行
            int sleepfor = 5000;
            Thread.Sleep(sleepfor);
            Console.WriteLine($"暂停{sleepfor/1000}秒");

终止线程

        static void Main(string[] args)
        {
            ThreadStart threadStart =new ThreadStart(CallToChildThread);
            Console.WriteLine("asd");
            Thread thread = new Thread(threadStart);
            Console.WriteLine("asdfg");
            thread.Start();//开启线程
            Thread.Sleep(1000);//延迟线程
            Console.WriteLine("1234");
            thread.Abort();//终止线程
            Console.ReadLine();

        }

线程先被中止了,后延迟的线程就报错了。

 

posted on 2023-07-24 23:20  阿霖找BUG  阅读(5)  评论(0编辑  收藏  举报