用於建立簡單執行緒的 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

更新時間: 19-Jun-2020

488 次瀏覽

啟動您的 職業

透過完成課程來獲得認證

開始
廣告