在進程中創(chuàng)建的第一個線程稱為主線程。它第一個開始,最后一個結(jié)束。
下面來看看 C# 中主線程的一個例子。參考以下示例代碼 -
using System;
using System.Threading;
public class ThreadExample
{
public static void Main(string[] args)
{
Thread t = Thread.CurrentThread;
t.Name = "MainThread";
Console.WriteLine(t.Name);
}
}
執(zhí)行上面示例代碼,得到以下結(jié)果 -
MainThread