如何在 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
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP