class NewThread : MyThread {
private string p1; private int p2;
public NewThread(string p1, int p2) { this.p1 = p1; this.p2 = p2; }
public override void Run() { Console.WriteLine(p1); Console.WriteLine(p2); }
}
new NewThread("hello world", 4321).Start();
IsBackground = true。所有前景執行緒結束即強制終止進程,不等待背景執行緒
Thread t = new Thread(MyMethod);
// t.IsBackground = true; // 取消註解變背景
t.Start();
若設為背景,主程式 3 秒 Sleep 後直接退出;否則等待 3 秒。
注意:必須在 Start() 前設定 IsBackground,啟動後不可變更。
透過 BeginInvoke 啟動的執行緒皆為背景執行緒。