使用system.threading.thread類可以創建和控制線程。
常用的構造函數有:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// 摘要: // 初始化 system.threading.thread 類的新實例,指定允許對象在線程啟動時傳遞給線程的委托。 // // 參數: // start: // system.threading.parameterizedthreadstart 委托,它表示此線程開始執行時要調用的方法。 // // 異常: // system.argumentnullexception: // start 為 null。 [securitysafecritical] public thread(parameterizedthreadstart start); // // 摘要: // 初始化 system.threading.thread 類的新實例。 // // 參數: // start: // system.threading.threadstart 委托,它表示此線程開始執行時要調用的方法。 // // 異常: // system.argumentnullexception: // start 參數為 null。 [securitysafecritical] public thread(threadstart start); |
1. 無參數創建線程
threadstart委托定義了一個返回類型位void的無參數方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public void main() { thread vthread = new thread(threadfun); //vthread.name = "td_name"; // 線程名稱 vthread.start(); //開始執行線程 console.writeline( "this is the main thread:id=" + thread.currentthread.managedthreadid); } void threadfun() // 來自委托:threadstart { console.writeline( "running in a new thread:id=" + thread.currentthread.managedthreadid); for ( int i = 0; i < 10; i++) { console.write( "." ); thread.sleep(500); } console.writeline( "thread end" ); } |
輸出結果:
將上訴代碼中的 threadfun() 用lambda表達式替換,變成thread的簡便使用方式:
1
2
3
4
5
6
7
8
9
10
|
public void main() { thread vthread = new thread(() => { console.writeline( "running in a new thread" ); }); //vthread.name = "td_name"; // 線程名稱 vthread.start(); //開始執行線程 console.writeline( "this is the main thread" ); } |
2.給線程傳遞參數
兩種方式:一種是使用帶parameterizedthreadstart委托的方法參數構造thread;另一種是創建一個自定義類,把線程的方法定義為實例方法,這樣先初始化實例的數據,在啟動線程。
如:傳遞參數
1
2
3
4
|
public struct tddata // 傳遞數據 { public string message; //數據string字段 } |
使用第一種方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public void main() { tddata tdata = new tddata() { message = "thread info" }; thread vthread = new thread(threadfun); vthread.start(tdata); // 開始執行線程,傳遞參數 console.writeline( "this is the main thread" ); } void threadfun( object pobj) // 來自委托:parameterizedthreadstart { tddata vdata = (tddata)pobj; console.writeline( "in a new thread, received:{0}" , vdata.message); } |
使用第二種方式:先自定義一個類。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class tdhelper { public tddata mdata; // 傳遞數據 // 構造函數 public tdhelper(tddata pdata) { this .mdata = pdata; } public void threadfun() // 來自委托:threadstart { console.writeline( "in a new thread, tddatamessage:{0}" , mdata.message); } } |
然后,在主線程(需要的地方)創建thread并將實例方法tdhelper.threadfun()作為構造函數的參數。
1
2
3
4
5
6
7
8
|
public void main() { tddata tdata = new tddata() { message = "thread info" }; tdhelper thelper = new tdhelper(tdata); // 傳遞參數 thread vthread = new thread(thelper.threadfun); vthread.start(); console.writeline( "this is the main thread" ); } |
3.后臺線程
默認情況下,thread類創建的線程事前臺線程,線程池中的線程總是后臺線程。只要有一個前臺線程在運行,應用程序的進程就在運行,如果多個前臺線程在運行,而main()方法結束了,應用程序仍然事激活的,直到所有前臺線程完成任務。
可以通過設置thread類實例的isbackground屬性,來讓其成為后臺線程;
1
2
3
4
5
6
7
8
9
10
11
12
|
public void main() { thread vthread = new thread(() => { console.writeline( "new thread started" ); // title3 thread.sleep(5000); console.writeline( "new thread completed" ); // title2 }); //vthread.isbackground = true; vthread.start(); console.writeline( "this is the main thread" ); // title1 } |
當isbackground屬性默認為false時,可以在控制臺完整地看到 3 句輸出信息;但如果將其設為true時,則不等到第3條信息(title2)輸出時,主線程main()已經執行完成,控制臺窗口就自動關閉了。
4.線程的優先級
通過priority屬性,可以調整thread類實例的優先級,默認為: vthread.priority = threadpriority.normal; // 枚舉值
關系:highest > abovenormal > normal > belownormal > lowest
5.控制線程
調用thread對象的start()方法,可以創建線程。但是,在調用start()方法后,新線程仍不是處于 running 狀態,而是 unstarted 狀態。只有操作系統的線程調度器選擇了要運行該線程,線程就會改為 running 狀態。通過 thread.threadstate 屬性,可以獲得該線程當前的狀態。
使用thread.sleep()方法,會使線程處于waitsleepjoin狀態,在經歷sleep()方法定義的時間段后,線程就會等待再次被操作系統調度。
要停止一個線程,可以調用 thread.abort() 方法。調用這個方法,會在接到終止命令的線程中拋出一個 threadabortexception,用一個處理程序捕獲這個異常,線程可以在結束前完成一些清理工作。線程還可以在接收到調用 thread.abort() 方法的結果 threadabortexception 異常后繼續工作。如果線程沒有重置終止,接收到終止請求的線程的狀態就從 abortrequested 改為 aborted 。
如果要等待線程結束,就可以調用 thread.join() 方法,它會停止當前線程,并把它設置為 waitsleepjoin 狀態,直到加入的線程完成為止。
參見: c#多線程之線程控制詳解
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/CUIT-DX037/p/6952092.html