在C#中创建和开始新主题我们就像下面这样:
using System.Threading;
Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
thread.Start();
public void WorkThreadFunction()
{
//Stuff Here
}
但是有参数的方法呢 对于这些方法,下面的代码有错误。
using System.Threading;
int a = 5;
int b = 6;
Thread thread = new Thread(new ThreadStart(WorkThreadFunction(a, b)));
thread.Start();
public void WorkThreadFunction(int a, int b)
{
//Stuff Here
}
错误:
预期的方法名称
我需要将这些参数传递给该方法!
解决办法是什么?
提前致谢
答案 0 :(得分:4)
使用lambda表达式封装使用参数调用方法:
Thread thread = new Thread(new ThreadStart(() => WorkThreadFunction(a, b)));