一种控制线程运行和停止的方法

 class Program
    {
        public static volatile bool _shouldStop=true; //可以再多线程里访问
        static void Main(string[] args)
        {
            ConsoleKey key; 

            Thread TestThread = new Thread(new Program().Run);
            TestThread.Start();

           Console.WriteLine("
输入Q键 暂停子线程");
           Console.WriteLine("
输入A键 运行子线程");
           do
           {
               key = Console.ReadKey().Key;
               if (key == ConsoleKey.Q)
                    Program._shouldStop =false;
               else if(key == ConsoleKey.A)
                    Program._shouldStop = true;
           }
           while (true);
            

        }
       
         public  void  Run()
        {
            while (true)
            {
                if (_shouldStop)
                {
                    Thread.Sleep(500);
                    Console.WriteLine("work thread: working ....");
                }
                else
                    Thread.Sleep(10);//通过线程的sleep来释放资源,否则会耗损系统资源,使CPU使用率过高
            }

            //Thread.CurrentThread.Abort();
          
        }
      
    }