簡單的多執行緒:
protectedvoid page_load(object
sender, eventargs e)
private
void mymethod(object
o)
使用委託的多執行緒:
publicpartial
class
demo : form
//定義委託
delegate
void mydelegate(string
str);
//宣告委託
mydelegate _mydelegate;
//宣告執行緒
private
thread t;
private
void demo_load(object
sender, eventargs e)
//執行委託
private
void
run()
);thread.sleep(
100);}}
//設定lable的text
private
void setlbltxt(string
o)
private
void btnbegin_click(object
sender, eventargs e)
private
void demo_formclosing(object
sender, formclosingeventargs e)
}}
有返回值的多執行緒:
//定義委託
delegate
string mydelegate(string
str);
//宣告委託
mydelegate _mydelegate;
//宣告執行緒
private
thread t;
private
void demo_load(object
sender, eventargs e)
//執行委託
private
void
run()
); //其實是同步呼叫
iasyncresult result = lblshow.begininvoke(_mydelegate, new
object
); //非同步呼叫
messagebox.show(lblshow.endinvoke(result).tostring());
//獲得返回值
thread.sleep(1000
); }}}
//設定lable的text
private
string setlbltxt(string
o)
private
void btnbegin_click(object
sender, eventargs e)
private
void demo_formclosing(object
sender, formclosingeventargs e)
}
c 委託與多執行緒
很多時候寫windows程式都需要結合多執行緒,在.net中用如下得 來建立並啟動乙個新的執行緒。public void threadproc thread thread new thread new threadstart threadproc thread.isbackground true t...
C Delegate 委託 與多執行緒
很多時候寫windows程式都需要結合多執行緒,在.net中用如下得 來建立並啟動乙個新的執行緒。public void threadproc thread thread new thread new threadstart threadproc thread.isbackground true t...
C Delegate 委託 與多執行緒
很多時候寫windows程式都需要結合多執行緒,在.net中用如下得 來建立並啟動乙個新的執行緒。public void threadproc thread thread new thread new threadstart threadproc thread.isbackground true t...