线程的挂起是通过Thread类的实例的Suspemd方法实现的,Suspend方法将无限期地挂起当前线程,如再次执行当前线程,就需要将当前的线程唤醒,这就要用到Thread类的实例的Resume方法来实现。

 

实例

通过线程的挂起与恢复来实现每隔一会就在屏幕上输出连续的一段">"

 

  public static void meth()
        {
            for (int i = 1; i < 400; i++)
            {
                if (i%40==0)
                {
                    Console.WriteLine(">");
                }
                else
                {
                    Console.Write(">");
                }
            }

        }
        static void Main(string[] args)
        {
            ThreadStart ts = new ThreadStart(meth);
             Thread ht=new Thread(ts);
            ht.Start();
            while (ht.IsAlive)
            {
                ht.Suspend();   //挂起
                Thread.Sleep(1000);
                ht.Resume();   //恢复
            }
            Console.Read();

 

相关文章:

  • 2022-12-23
  • 2021-10-18
  • 2022-02-03
  • 2021-12-12
  • 2021-09-12
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-05
  • 2021-12-17
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
相关资源
相似解决方案