C# 程序来实现线程的睡眠方法
线程的睡眠方法用于在特定时间段内暂停线程。
如果你想让线程休眠数秒,则像以下代码片段一样使用它 −
int sleepfor = 2000; Thread.Sleep(sleepfor);
示例
你可以尝试运行以下代码来实现线程的睡眠方法。
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