C# 通过ParameterizedThreadStart传递参数
我试图通过以下方式传递参数:C# 通过ParameterizedThreadStart传递参数,c#,multithreading,C#,Multithreading,我试图通过以下方式传递参数: Thread thread = new Thread(new ParameterizedThreadStart(DoMethod)); 你知道怎么做吗?如果您能提供一些帮助,我将不胜感激。使用重载方法,该方法接受对象(如果需要几个参数,您可以传递自定义类型或数组): 在DoMethod中,只需将参数强制转换为参数类型: private void DoMethod(object obj) { Foo parameter = (Foo)obj; //
Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));
你知道怎么做吗?如果您能提供一些帮助,我将不胜感激。使用重载方法,该方法接受对象(如果需要几个参数,您可以传递自定义类型或数组):
在DoMethod
中,只需将参数强制转换为参数类型:
private void DoMethod(object obj)
{
Foo parameter = (Foo)obj;
// ...
}
顺便说一句在.NET 4.0及更高版本中,您可以使用任务(也要注意竞赛条件):
拉兹别列佐夫斯基的答案是正确的。我想指出,从技术上讲,由于变量捕获,您可以使用lambda表达式传递任意数量的参数:
var thread = new Thread(
() => DoMethod(a, b, c));
thread.Start();
这是调用不适合
ThreadStart
或ParameterizedThreadStart
委托的方法的一种简便方法,但请注意,如果在将父线程中的参数传递给子线程的代码后更改它们,则很容易导致数据争用。另一种归档所需内容的方法,是通过在函数/方法中返回委托实现的。以以下为例:
// Parameters to pass to ParameterizedThreadStart delegate
// - in this example, it's an Int32 and a String:
class MyParams
{
public int A { get; set; }
public string B { get; set; }
// Constructor
public MyParams(int someInt, string someString)
{
A = someInt;
B = someString;
}
}
class MainClass
{
MyParams ap = new MyParams(10, "Hello!");
Thread t = new Thread(new ParameterizedThreadStart(DoMethod));
t.Start(ap); // Pass parameters when starting the thread
}
class App
{
public static void Main()
{
Thread t = new Thread(DoWork(a, b));
t.Start();
if (t.IsAlive)
{
t.IsBackground = true;
}
}
private static ThreadStart DoWork(int a, int b)
{
return () => { /*DoWork*/ var c = a + b; };
}
}
或
您可以使用匿名类型,然后只使用动态类型来提取参数,而不是像@user1958681那样创建一个类来传递多个参数
class MainClass
{
int A = 1;
string B = "Test";
Thread ActionThread = new Thread(new ParameterizedThreadStart(DoWork));
ActionThread.Start(new { A, B});
}
然后是嫁妆
private static void DoWork(object parameters)
{
dynamic d = parameters;
int a = d.A;
string b = d.B;
}
使用thread.Start(paramValue)开始。我有点困惑。为什么使用此方法比使用ParameterizedThreadStart或ThreadStart方法更可能发生数据竞争?@2位:因为使用此方法时,变量通过引用捕获,因此父线程中的任何更改都会影响子线程中的值。此外,变量是在lamda实际执行时捕获的,而不是线程启动时,如果在父线程中修改了值,则会为数据竞争提供一个很小的窗口。所以这里有一个闭包?这不是问题的答案,有些情况下,你不想使用lambdadooe,因为它似乎与其他答案没有区别。
new Thread(() => { DoMethod(a, b, c); }).Start();
new Thread(() => DoMethod(a, b, c)).Start();
class MainClass
{
int A = 1;
string B = "Test";
Thread ActionThread = new Thread(new ParameterizedThreadStart(DoWork));
ActionThread.Start(new { A, B});
}
private static void DoWork(object parameters)
{
dynamic d = parameters;
int a = d.A;
string b = d.B;
}
class Program
{
public static void Main()
{
MyClass myClass = new MyClass();
ParameterizedThreadStart pts = myClass.DoMethod;
Thread thread1 = new Thread(pts);
thread1.Start(20); // Pass the parameter
Console.Read();
}
}
class MyClass
{
private int Countdown { get; set; }
public void DoMethod(object countdown) // Parameter must be an object and method must be void
{
Countdown = (int) countdown;
for (int i = Countdown; i > 0; i--)
{
Console.WriteLine("{0}", i);
}
Console.WriteLine("Finished!");
}
}