您的位置:首页 > 其它

挂起起线程与终止线程的区别

2015-10-26 12:54 405 查看
Program1 p1 = new Program1(); Thread th = new Thread(new ThreadStart(p1.ThreadRunMethord));//创建一个新的子线程 if (th.ThreadState != ThreadState.Running)//判断子线程未开始时运行 { th.Start();//启动子线程 } Thread.Sleep(1000);//主线程休眠1秒钟 Console.WriteLine("子线程被挂起而暂停"); if (th.ThreadState == ThreadState.Running)//若子线程为运行状态 { th.Suspend();//线程被挂起而暂停 } if (th.ThreadState == ThreadState.Suspended)//若子线程为挂起状态 { th.Resume();//恢复子线程而继续执行 } Console.WriteLine("恢复运行已挂起的子线程");//输出子线程已被恢复执行的信息 Console.ReadLine(); public void ThreadRunMethord()//子线程执行的方法 { Console.WriteLine("子线程开始运行...."); } Program1 p1 = new Program1(); Thread th = new Thread(new ThreadStart(p1.ThreadRunMethord));//创建一个新的子线程 if (th.ThreadState != ThreadState.Running)//判断子线程未开始时运行 { th.Start();//启动子线程 } Thread.Sleep(1000);//主线程休眠1秒钟 Console.WriteLine("子线程被挂起而暂停"); if (th.ThreadState == ThreadState.Running)//若子线程为运行状态 { th.Abort();//子线程运行终止 } if (th.ThreadState == ThreadState.Stopped)//若子线程已经停止 { Console.WriteLine("线程已经终止");//输出子线程终止的信息 } Console.ReadLine(); public void ThreadRunMethord()//子线程执行的方法 { Console.WriteLine("子线程开始运行...."); }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: