1 Thread t; 2 public myThread() 3 { 4 t = new Thread(run); 5 t.Start(2); //这里吧参数传递进去 6 t.Join(); 7 8 } 9 static void run(object obj) //带参数的run函数 10 { 11 int a = (int)obj; 12 Console.WriteLine(a); 13 14 }
namespace ThreadPara 2 { 3 class myThread //直接传参数进去 4 { 5 Thread t; 6 public myThread() 7 { 8 t = new Thread(run); 9 t.Start(2); 10 t.Join(); 11 12 } 13 static void run(object obj) 14 { 15 int a = (int)obj; 16 Console.WriteLine(a); 17 18 } 19 } 20 class MyThreadfun 21 { 22 public int a; 23 public int b; //通过公共成员来赋值 24 public void fun() 25 { 26 int c = a + b; 27 Console.WriteLine(c); 28 } 29 30 } 31 class Program 32 { 33 static void Main(string[] args) 34 { 35 myThread mt = new myThread(); 36 MyThreadfun fun = new MyThreadfun(); 37 Thread t = new Thread(fun.fun); 38 fun.a = 2; 39 fun.b = 3; 40 t.Start(); 41 42 } 43 } 44 }