如何在C#中使用sleep方法?
线程的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
热门推荐
2 庆双节祝福语简短
10 浪漫的生日祝福语简短
11 三八简短祝福语给婆婆
12 师姐毕业送花祝福语简短
13 迎接朋友蛋糕祝福语简短
14 分手毕业祝福语简短女生
15 近代生日祝福语简短精辟
16 参加儿子大学祝福语简短
17 幼教专干调动祝福语简短
18 新年祝福语简短给孩子