C# 程式建立簡單執行緒


為了建立一個執行緒,我建立了一個函式 -

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

更新於:2020 年 6 月 19 日

489 次瀏覽

開啟你的 職業 生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.