How Start Methods With Arguments Using Thread

主宰稳场 提交于 2019-12-11 11:45:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!