(一).描述
由于線程只能執行無參數方法. 有時候需要線程執行"帶參數方法"
此示例演示怎樣借助封裝類實現“線程調用帶參方法”
(二).代碼
本示例代碼已經測試,能夠正常運行!
(三).示例下載
http://www.cnblogs.com/files/chengking/threadexample.rar
using system;
using system.threading;
namespace 借助封裝類實現_線程調用帶參方法_
{
class help
{
public int x = 0; //乘數1
public int y = 0; //乘數2
public int end = 0; //存放結果
}
class myclass
{
public static help myhelp = new help();
[stathread]
static void main(string[] args)
{
//給類的成員賦值
myhelp.x = 5;
myhelp.y = 10;
thread thread = new thread(new threadstart(getaccumulate));
thread.start();
thread.sleep(1000); //主線程等待子線程計算完成,否則取得的值為默認值 : 0
console.writeline("兩數乘積結果為: "+myhelp.end.tostring());
console.read();
}
///
/// 得到兩個整數的集
///
/// 乘數x
/// 乘數y
/// x*y
public static void getaccumulate()
{
myhelp.end = myhelp.x * myhelp.y;
}
}
}
新聞熱點
疑難解答