C# 程序暂停一个线程


要在 C# 中暂停一个线程,请使用 sleep() 方法。

需要设置线程暂停的毫秒数,例如,要暂停 5 秒,请使用 -

Thread.Sleep(5000);

示例

让我们看看如何循环并设置睡眠方法以暂停线程。

在线演示

using System;
using System.Threading;
namespace Sample {
   class Demo {
      static void Main(string[] args) {
         for (int i = 0; i < 10; i++) {
            Console.WriteLine("Sleep for 1 second!");
            Thread.Sleep(1000);
         }
         Console.ReadLine();
      }
   }
}

输出

Sleep for 1 second!
Sleep for 1 second!
Sleep for 1 second!
Sleep for 1 second!
Sleep for 1 second!
Sleep for 1 second!
Sleep for 1 second!
Sleep for 1 second!
Sleep for 1 second!
Sleep for 1 second!

更新于:19-06-2020

7K+ 次浏览

开启您的 职业生涯

学习课程并获得认证

开始学习
Advertisement
© . All rights reserved.