如何在 C# 中建立執行緒?
執行緒是輕量級的程序。執行緒被定義為程式的執行路徑。透過擴充套件 Thread 類建立執行緒。然後,擴充套件的 Thread 類呼叫 Start() 方法來開始子執行緒執行。
執行緒示例:一個常見的執行緒使用示例是指由現代作業系統實施的併發程式設計。使用執行緒可以節省 CPU 週期浪費,並提高應用程式的效率。
以下是示範如何建立執行緒的一個示例。
示例
using System; using System.Threading; namespace Demo { class Program { public static void ThreadFunc() { Console.WriteLine("Child thread starts"); } static void Main(string[] args) { ThreadStart childref = new ThreadStart(ThreadFunc); 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
廣告