如何在 C# 中使用睡眠方法?
线程的 sleep 方法用于暂停线程特定时间。
如果你想要设置睡眠时间为几秒钟,那么使用如下的代码片段 −
int sleepfor = 2000; Thread.Sleep(sleepfor);
你可以试着运行下面的代码来实现 sleep 方法 −
示例
using System;
using System.Threading;
namespace MyApplication {
class ThreadCreationProgram {
public static void CallToChildThread() {
Console.WriteLine("Child thread starts");
int sleepfor = 2000;
Console.WriteLine("Child Thread Paused for {0} seconds", sleepfor / 1000);
Thread.Sleep(sleepfor);
Console.WriteLine("Child thread resumes");
}
static void Main(string[] args) {
ThreadStart childref = new ThreadStart(CallToChildThread);
Console.WriteLine("In Main: Creating the Child thread");
Thread childThread = new Thread(childref);
childThread.Start();
Console.ReadKey();
}
}
}输出
In Main: Creating the Child thread Child thread starts Child Thread Paused for 2 seconds Child thread resumes
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP