![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class ThreadSample { private readonly int _number; public ThreadSample(int number) { _number = number; } public void CountNumber() { for (int i = 0; i < _number; i++) { Thread.Sleep(TimeSpan.FromSeconds(1)); Console.WriteLine("Current Thread {0} print {1}", Thread.CurrentThread.Name, i); } } }
static void Main(string[] args) { ThreadSample foreGround = new ThreadSample(10); ThreadSample backGround = new ThreadSample(30); Thread foreThread = new Thread(foreGround.CountNumber); foreThread.Name = "ForeThread"; //foreThread.IsBackground = false; Thread backThread = new Thread(backGround.CountNumber); backThread.Name = "BackThread"; backThread.IsBackground = true; foreThread.Start(); backThread.Start(); Console.ReadLine(); //这行要注释掉,否则后台进程会继续进行 }