如何将带有一个参数的方法放入Thread C#。
示例:
Thread thread = new Thread(SoundInputThread.getSomething(hz));
thread.Start();
for (int i = 0; i < 5; i++) {
Console.WriteLine();
Thread.Sleep(1000);
}
public static void getSomething(int hz) {
hz = 100;
Console.WriteLine();
}
答案 0 :(得分:3)
您可以按如下方式捕获值:
Thread thread = new Thread(() => {
getSomething(hz);
});
thread.Start();
答案 1 :(得分:1)
你需要使用Thread的重载构造函数,它接受ParameterizedThreadStart它将允许将参数传递给线程方法。在该方法中,您可以将对象重新设置为您的类型。
thread = new Thread(new ParameterizedThreadStart(getSomething));
thread.Start(2);
public static void getSomething(object obj) {
int i = (int)obj;
}