帶引數的多執行緒的方式

2021-09-08 16:54:08 字數 1754 閱讀 4281

#region 執行帶乙個引數的多執行緒thread mythread = new thread(new

parameterizedthreadstart(calculate));

mythread.isbackground = true

;

mythread.start(

500);

#endregion

private

void calculate(object max) //

帶乙個引數的委託函式

stopwatch.stop();

long lsearchtime =stopwatch.elapsedmilliseconds;

messagebox.show(lsearchtime.tostring() + "毫秒"

); }

方式一: 定義乙個類,將要傳的引數設定為類的屬性,然後將引數值賦值給類的屬性,將類作為乙個引數進行傳達,以下**通過兩個引數示例,多個引數一樣,**如下

class

myclass

public

int min

}#region 第一種方式:執行帶多個引數的多執行緒myclass model = new

myclass();

model.max = 500

; model.min = 0

; thread mythread1 = new thread(new

parameterizedthreadstart(calculatetwo));

mythread1.isbackground = true

;

mythread1.start(model);

#endregion

private

void calculatetwo(object myclass) //

帶多個引數的委託函式

stopwatch.stop();

long lsearchtime =stopwatch.elapsedmilliseconds;

messagebox.show(lsearchtime.tostring() + "毫秒"

); }

方式二:lambda表示式的方式,簡單方便,**如下:

#region 第二種方式:執行帶多個引數的多執行緒thread mythread2 = new thread(() => calculatethree(500, 0

)); mythread2.isbackground = true; //

設定為後台執行緒,程式關閉後程序也關閉,如果不設定true,則程式關閉,此執行緒還在記憶體,不會關閉

mythread2.start();

#endregion

private

void calculatethree(int max,int min) //

帶多個引數的委託函式

stopwatch.stop();

long lsearchtime =stopwatch.elapsedmilliseconds;

messagebox.show(lsearchtime.tostring() + "毫秒"

); }

帶引數的多執行緒的方式

region 執行帶乙個引數的多執行緒thread mythread new thread new parameterizedthreadstart calculate mythread.isbackground true mythread.start 500 endregion private v...

帶引數的多執行緒的方式

通常會有需求通過多執行緒呼叫帶引數的委託函式,有乙個引數的,也有多個引數的,下面來講講實現的方式 region 執行帶乙個引數的多執行緒thread mythread new thread new parameterizedthreadstart calculate mythread.isbackg...

帶引數的多執行緒如何去寫?

帶引數的多執行緒的方式 region 執行帶乙個引數的多執行緒 thread mythread new thread new parameterizedthreadstart calculate mythread.isbackground true mythread.start 500 endreg...