我必须将多个参数传递给一个线程.我把它们包装成了一个类对象.我希望在其中传递一个变量(双数组)作为参考(我期望在此变量中得到结果).怎么可能?
class param
{
int offset = 0;
double[] v = null;
}
param p = new param();
p.v = new double[100]; // I want to pass this(v) variable by reference
p.offset = 50;
....
thread1.Start(p);
Run Code Online (Sandbox Code Playgroud)
有很多解决方案.一个是,你可以使用ParameterizedThreadStart它.
param p = new param();
// start the thread, and pass in your variable
Thread th = new Thread(new ParameterizedThreadStart(MyThreadMethod));
th.Start(p);
public void MyThreadMethod(object o)
{
// o is your param
param pv = (param)o;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12460 次 |
| 最近记录: |