C# 中的主线程与子线程

csharpprogrammingserver side programming更新于 2025/8/12 14:52:17

主线程

进程中第一个执行的线程称为主线程。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

相关文章