我們先從最基礎的Thread說起。 創建並啟動線程 創建並啟動一個線程,如下代碼: 1 namespace ConsoleApplication17 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 var thread ...
我們先從最基礎的Thread說起。
創建並啟動線程
創建並啟動一個線程,如下代碼:
1 namespace ConsoleApplication17 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 var thread = new Thread(PrintNumbers); 8 thread.Start(); 9 10 Console.WriteLine("Thread Start..."); 11 Console.ReadKey(); 12 } 13 14 /// <summary> 15 /// 匹配委托的方法 16 /// </summary> 17 public static void PrintNumbers() 18 { 19 Console.WriteLine("Starting......"); 20 for (int i = 0; i < 10; i++) 21 { 22 Console.WriteLine(i); 23 } 24 } 25 } 26 }View Code
運行結果:
暫停線程
假如需要暫停當前線程,可以調用Thread.Sleep方法,使當前線程處於阻塞狀態,如下代碼:
1 namespace ConsoleApplication17 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 var thread = new Thread(PrintNumbersWithDelay); 8 thread.Start(); 9 10 Console.WriteLine("Thread Start..."); 11 Console.ReadKey(); 12 } 13 14 /// <summary> 15 /// 16 /// </summary> 17 public static void PrintNumbersWithDelay() 18 { 19 Console.WriteLine("Starting......"); 20 for (int i = 0; i < 10; i++) 21 { 22 Thread.Sleep(TimeSpan.FromMilliseconds(1000)); 23 Console.WriteLine(string.Format("{0} {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), i)); 24 } 25 } 26 } 27 }View Code
輸出結果:
合併線程
如果需要等待某個子線程執行行,主線程才繼續執行時,可以使用Thread.Join方法來實現,如下代碼:
1 namespace ConsoleApplication17 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 //Thread 8 var thread = new Thread(PrintNumbersWithDelay); 9 thread.Start(); 10 thread.Join(); 11 12 Console.WriteLine("Thread Completed!"); 13 Console.ReadKey(); 14 } 15 16 /// <summary> 17 /// 18 /// </summary> 19 public static void PrintNumbersWithDelay() 20 { 21 Console.WriteLine("Starting......"); 22 for (int i = 0; i < 10; i++) 23 { 24 Thread.Sleep(TimeSpan.FromMilliseconds(1000));//線程阻塞1s,此時線程狀態為WaitSleepJoin 25 Console.WriteLine(string.Format("當前時間:{0},線程狀態:{1},結果:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),Thread.CurrentThread.ThreadState, i)); 26 } 27 } 28 } 29 }View Code
輸出結果:
終止線程
如果在子線程運行過程中強制終止它,可以調用Thread.Abort方法,這會給當前子線程觸發ThreadAbortException異常,導致線程被終止!
如下代碼:
1 namespace ConsoleApplication17 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Console.WriteLine("Starting Program..."); 8 var thread = new Thread(PrintNumbersWithDelay); 9 thread.Start(); 10 11 Thread.Sleep(TimeSpan.FromMilliseconds(6000)); 12 thread.Abort(); 13 14 Console.WriteLine("Thread has been abort!"); 15 Console.ReadKey(); 16 } 17 18 /// <summary> 19 /// 20 /// </summary> 21 public static void PrintNumbersWithDelay() 22 { 23 Console.WriteLine("Starting......"); 24 for (int i = 0; i < 10; i++) 25 { 26 Thread.Sleep(TimeSpan.FromMilliseconds(1000)); 27 Console.WriteLine(string.Format("當前時間:{0},線程狀態:{1},結果:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ThreadState, i)); 28 } 29 } 30 } 31 }View Code
線程傳遞參數
通過分析可以發現,Thread接受的實際上是一個委托,包括無參數的委托和接受一個Object類型的委托,
如下代碼:
1 namespace ConsoleApplication17 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Console.WriteLine("Main thread starting..."); 8 var thread = new Thread(PrintNumbersWithCount); 9 thread.Start(5); 10 thread.Join(); 11 12 Console.WriteLine("Main thread completed!"); 13 Console.ReadKey(); 14 } 15 16 /// <summary> 17 /// 匹配委托方法,帶參數 18 /// </summary> 19 public static void PrintNumbersWithCount(object obj) 20 { 21 Console.WriteLine("Sub thread starting..."); 22 var number = Convert.ToInt32(obj); 23 for (int i = 0; i < number; i++) 24 { 25 Console.WriteLine(string.Format("當前時間:{0},線程狀態:{1},結果:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ThreadState, i)); 26 } 27 } 28 } 29 }View Code
輸出結果: