C# 中的主執行緒與子執行緒


主執行緒

程序中首先執行的執行緒稱為主執行緒。當 C# 程式開始執行時,會自動建立主執行緒。

子執行緒

使用 Thread 類建立的執行緒稱為主執行緒的子執行緒。

以下是一個示例,展示如何建立主執行緒和子執行緒 −

示例

 即時演示

using System;
using System.Threading;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         Thread th = Thread.CurrentThread;
         th.Name = "MainThread";
         Console.WriteLine("This is {0}", th.Name);
         Console.ReadKey();
      }
   }
}

輸出

This is MainThread

更新於:22-6-2020

685 次瀏覽

開啟你的職業生涯

透過完成課程獲取認證

開始學習
廣告
© . All rights reserved.