給執行緒傳遞引數

2021-05-22 16:25:02 字數 2398 閱讀 3210

執行緒操作主要用到thread類,他是定義在system.threading.dll下。使用時需要新增這乙個引用。該類提供給我們四個過載的建構函式(以下引自msdn)。

我們如果定義不帶引數的執行緒,可以用threadstart;帶乙個引數的用parameterizedthreadstart。帶多個引數的用另外的方法,下面逐一講述。

一、不帶引數的

view plaincopy to clipboardprint?

using system;   

using system.collections.generic;   

using system.text;   

using system.threading;   

namespace aaaaaa   

private static void a()   

}   

}  

using system;

using system.collections.generic;

using system.text;

using system.threading;

namespace aaaaaa

private static void a() }

} 結果顯示method a!

二、帶乙個引數的

由於parameterizedthreadstart要求引數型別必須為object,所以定義的方法b形參型別必須為object。

view plaincopy to clipboardprint?

using system;   

using system.collections.generic;   

using system.text;   

using system.threading;   

namespace aaaaaa   

private static void b(object obj)   

!",obj.tostring ());   

}   

}   

}  

using system;

using system.collections.generic;

using system.text;

using system.threading;

namespace aaaaaa

private static void b(object obj)

!",obj.tostring ());

} }

} 結果顯示method b!

三、帶多個引數的

由於thread預設只提供了這兩種建構函式,如果需要傳遞多個引數,我們可以自己將引數作為類的屬性。定義類的物件時候例項化這個屬性,然後進行操作。

+ expand sourceview plaincopy to clipboardprint?

using system;   

using system.collections.generic;   

using system.text;   

using system.threading;   

namespace aaaaaa   

}   

class my   

,y=", this.x, this.y);   

}   

}   

}  

using system;

using system.collections.generic;

using system.text;

using system.threading;

namespace aaaaaa }

class my

,y=", this.x, this.y);

} }

} 結果顯示x=2,y=3

四、利用結構體給引數傳值。

定義公用的public struct結構體,裡面可以定義自己需要的引數,然後在需要新增執行緒的時候,可以定義結構體的例項。

view plaincopy to clipboardprint?

//結構體   

struct rowcol   

;   

//定義方法   

public void output(object rc)   

", _char);   

console.write("/n");   

}   

Mysql給in傳遞引數

情景 select from student where name in 李四 王五 張三 查詢學生表中name欄位是 李四 王五 張三 的資料需求 李四 王五 張三 這些字段需要前端傳過來且數量不確定方法 使用find in set name,第乙個引數是列名,第二個引數是字串型別的,接收傳遞的資...

如何給OPENQUERY傳遞引數

發現sql server裡,直接呼叫鏈結伺服器的函式是不可以的。create function func test userid int returns table asreturn select from 192.168.0.252 user dbo.func getdata userid go ...

Spring給通知傳遞引數

切點 override public void printaccount account account catch exception ex 切面 package com.itlearn.aspect import com.itlearn.domain.account import org.asp...