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-6-2020

7 千多次浏览

开启你的职业生涯

完成课程,通过认证

开始
广告
© . All rights reserved.