為了建立線程,我建立了一個函數-
public void myThread() { for (int i = 0; i < 3; i++) { Console.WriteLine("My Thread"); } }
呼叫上面的函數來建立一個線程,並建立一個新的ThreadStart 委託-
Demo d = new Demo(); Thread thread = new Thread(new ThreadStart(d.myThread));
使用以下程式碼建立一個簡單的執行緒。
即時示範
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; class Demo { public void myThread() { for (int i = 0; i < 3; i++) { Console.WriteLine("My Thread"); } } } class NewThread { public static void Main() { Demo d = new Demo(); Thread thread = new Thread(new ThreadStart(d.myThread)); thread.Start(); Console.Read(); } }
My Thread My Thread My Thread
以上是創建簡單線程的 C# 程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!