问题
For Create And Start New Threads In C# We Act Like Below :
using System.Threading;
Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
thread.Start();
public void WorkThreadFunction()
{
//Stuff Here
}
but what about methods with arguments.
for these methods the codes below have an error.
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
}
ERROR :
Method name expected
i need to pass those parameters to that method!
what is the solution?
thanks in advance
回答1:
Use a lambda expression for encapsulating the invocation of your method with parameters:
Thread thread = new Thread(new ThreadStart(() => WorkThreadFunction(a, b)));
来源:https://stackoverflow.com/questions/11064236/how-start-methods-with-arguments-using-thread