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(); }
线程先被中止了,后延迟的线程就报错了。
本文来自博客园,作者:阿霖找BUG,转载请注明原文链接:https://www.cnblogs.com/lin-07/p/17578636.html