我想将一个函数(a)作为参数传递给方法(b),然后开始执行该函数(a)的新线程。类似于:
Thread tr = new Thread(()=>SomeMethodWithParams(params))
但是就像:
public void SomeMethod(System.Func<string> SomeMethodWithParams)
{
Thread tr = new Thread(SomeMethodWithParams);//how to do this right??
tr.Start();
}
我想这样做,
public void RunThrededJobs(Action[] actions)
{
for(int i =0; i<actions.Length; i++)
{
ThreadStart x = new ThreadStart(actions[i]);
Thread someThread = new Thread(x);
someThread.IsBackground = true;
someThread.Start();
}
}
但在新线程中执行的方法中带有参数。
答案 0 :(得分:0)
您可以通过几种不同的方式来处理此问题,但是我不清楚您到底需要如何使用它。但是,给定RunThrededJobs
的函数定义,您可以像这样执行它:
Action[] actions = {
() => SomeMethodWithParams("foo"),
() => SomeMethodWithParams("bar"),
() => SomeMethodWithParams("baz")
};
RunThrededJobs(actions);