通过suspend()函数,可使线程进入停滞状态。通过suspend()使线程进入停滞状态后,除非收到resume()消息,否则该线程不会变回可执行状态。当调用suspend()函数后,线程不会释放它的“钥匙”。
在开启子线程后立即让他挂起,直到执行了Resume()后恢复线程的执行。注意如果在线程没有挂起时去调用Resume()方法会出现异常,所有使用这样的方法进行线程线程同步已经不推荐使用了。
- class Program
- {
- private static Thread subthread ;
- private static string name ="";
- static void Main(string[] args)
- {
- subthread = new Thread(new ThreadStart(GetShow));
- subthread.IsBackground = false;
- subthread.Name = "子线程";
- subthread.Start(); //开启线程
- subthread.Suspend(); //挂起
- Console.WriteLine(subthread.Name + "挂起");
- Console.WriteLine("{0}后台线程", Thread.CurrentThread.Name+Thread.CurrentThread.IsBackground+",结束");
- subthread.Resume(); //执行
- Console.WriteLine("主线程结束");
- }
- static void GetShow()
- {
- Console.WriteLine("输入姓名:");
- name = Console.ReadLine();
- Console.WriteLine("执行");
- }
- }