一个参数:
Thread.Start方法可以带一个参数:
public static void Main()
{ Thread t = new Thread(new ParameterizedThreadStart(B)); t.Start("B"); Console.Read(); } private static void B(object obj) { Console.WriteLine("Method {0}!",obj.ToString ()); }
多个参数:
1、将线程执行过程封装成一个类,将使用到的参数设置为类的变量或属性。使用时,将先声明并实体化一个执行类,并将其属性赋值,然后再创建线程调用其执行方法。
public static void Main()
{ My m = new My(); m.x = 2; m.y = 3; Thread t = new Thread(new ThreadStart(m.C)); t.Start(); Console.Read(); } class My { public int x, y; public void C() { Console.WriteLine("x={0},y={1}", this.x, this.y); } }
2、使用委托
private static void Main() { Thread thread = new Thread(new ThreadStart(delegate{ Flash(a,b); })); thread.IsBackground = true; thread.Start();
} private static void Flash(int a,string b) { Console.WrilteLine("a is {0},b is {1}",a,b); }