C#多线程传参
class test
{
private int i ;
private string s;
public test(int n1,string s1)
{
this.i = n1;
this.s = s1;
}
public void Withparameters()
{
Console.WriteLine("线程启动,参数{0},{1}",i,s);
}
}
class Program
{
static void Main()
{
Thread th = new Thread(new ParameterizedThreadStart(WithParameters));//使用parameterizedThreadStart能传递一个object参数。可以对object参数进行转化,实现多个参数传递
th.Start("ParameterizedthreadStart");
test t=new test(123,"参数");//自定义类可传递多个参数
Thread th1 = new Thread(t.Withparameters);
th1.Start();
Thread th2 = new Thread(() => WithParameters("lambda"));//lambda匿名方法可传递多个参数。
th2.Start();
Console.ReadKey();
}
static void WithParameters(object obj)
{
string o = obj as string;
if (!string.IsNullOrEmpty(o))
{
Console.WriteLine("线程启动,参数{0}",o);
}
}
}