如何使C#程序睡眠x毫秒?
若要使C#程序休眠x毫秒,请使用Thread.Sleep()方法。
将其设置为1000毫秒-
Thread.Sleep(1000);
以下是显示代码的代码,该代码如何在for循环的每次迭代中为线程设置计数器并将其设置为休眠1000毫秒-
示例
using System;
using System.Threading;
namespace MultithreadingApplication {
public class ThreadCreationProgram {
public static void CallToChildThread() {
try {
Console.WriteLine("Child thread starts");
for (int counter = 0; counter <= 10; counter++) {
Thread.Sleep(1000);
Console.WriteLine(counter);
}
Console.WriteLine("Child Thread Completed");
} catch (ThreadAbortException e) {
Console.WriteLine("Thread Abort Exception");
} finally {
Console.WriteLine("Couldn't catch the Thread Exception");
}
}
public 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();
//停止主线程一段时间
Thread.Sleep(5000);
}
}
}输出结果
In Main: Creating the Child thread Child thread starts 0 1 2 3 4 5 6 7 8
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短