黑馬程序員+多線程基礎
1 單線程的缺點:一次只能做一件事(ui線程)
2 方法作為參數,要用到委托(線程啟動時,調用傳過來的委托,委托就會執行相應的方法,實現線程執行方法)
3產生一個線程的4步驟:
a: 編寫產生線程所要執行的方法 b:引用system.Threading命名空間 c:實例化thread類,并傳入一個指向線程所要運行方法的委托 d:調用thread實例的start方法,標記該線程可以被cpu執行了
4前臺線程:只有所有的前臺線程都關閉了才能完成程序的關閉
后臺線程:只要所有的前臺線程都關閉了就會自動結束 thread.isbackground=true
5 TextBox.CheckForIllegalCrossThreadCalls = false;
6 多線程不帶參數:
Thread thread = new Thread(Count);
thread.IsBackground = true;
thread.Start();
7 多線程帶參數:
ParameterizedThreadStart pts = new ParameterizedThreadStart(getName);
Thread thread2 = new Thread(pts);
thread2.IsBackground = true;
thread2.Start(“kk”);
8多線程帶多參數: 采用泛型來實現
void getName(object name) {
List<string> list = name as List<string>;
foreach (var item in list)
{
MessageBox.Show(item.ToString());
}
}
ParameterizedThreadStartpts=newParameterizedThreadStart(getName);
Threadthread2= newThread(pts);
thread2.IsBackground=true;
thread2.Start(newList<string>{ "tt","kk"});
posted on 2013-11-03 19:48 張亮13128600812 閱讀(141) 評論(0) 收藏 舉報
浙公網安備 33010602011771號