C#,往线程里传参数的方法总结

作者:互联网   出处:控件中国网   2014-11-05 19:21:53   阅读:1

C#,往线程里传参数的方法总结

Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。  
Thread (ThreadStart) 初始化 Thread 类的新实例。 
由 .NET Compact Framework 支持。 
Thread (ParameterizedThreadStart, Int32) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。  
Thread (ThreadStart, Int32) 初始化 Thread 类的新实例,指定线程的最大堆栈大小。 
由 .NET Compact Framework 支持。 
  我们如果定义不带参数的线程,可以用ThreadStart,带一个参数的用ParameterizedThreadStart。带多个参数的用另外的方法,下面逐一讲述。 
一、不带参数的 

 

view sourceprint?01 using System;   

02 using System.Collections.Generic;   

03 using System.Text;   

04 using System.Threading;   

05   

06 namespace AAAAAA   

07 {   

08   class AAA   

09   {   

10   public static void Main()   

11   {   

12   Thread t = new Thread(new ThreadStart(A));   

13   t.Start();   

14   

15   Console.Read();   

16   }   

17   

18   private static void A()   

19   {   

20   Console.WriteLine("Method A!");   

21   }   

22   }   

23 }

结果显示Method A! 

二、带一个参数的 
  由于ParameterizedThreadStart要求参数类型必须为object,所以定义的方法B形参类型必须为object。 

 

view sourceprint?01 using System;   

02 using System.Collections.Generic;   

03 using System.Text;   

04 using System.Threading;   

05   

06 namespace AAAAAA   

07 {   

08   class AAA   

09   {   

10   public static void Main()   

11   {    

12   Thread t = new Thread(new ParameterizedThreadStart(B));   

13   t.Start("B");   

14   

15   Console.Read();   

16   }   

17   

18   private static void B(object obj)   

19   {   

20   Console.WriteLine("Method {0}!",obj.ToString ());   

21   

22   }   

23   }   

24 }

结果显示Method B! 

 

三、带多个参数的 
  由于Thread默认只提供了这两种构造函数,如果需要传递多个参数,我们可以自己将参数作为类的属性。定义类的对象时候实例化这个属性,然后进行操作。 

 

view sourceprint?01 using System;   

02 using System.Collections.Generic;   

03 using System.Text;   

04 using System.Threading;   

05   

06 namespace AAAAAA   

07 {   

08   class AAA   

09   {   

10   public static void Main()   

11   {   

12   My m = new My();   

13   m.x = 2;   

14   m.y = 3;   

15   

16   Thread t = new Thread(new ThreadStart(m.C));   

17   t.Start();   

18   

19   Console.Read();   

20   }   

21   }   

22   

23   class My   

24   {   

25   public int x, y;   

26   

27   public void C()   

28   {   

29   Console.WriteLine("x={0},y={1}", this.x, this.y);   

30   }   

31   }   

32 }

结果显示x=2,y=3 

 

四、利用结构体给参数传值。 
定义公用的public struct,里面可以定义自己需要的参数,然后在需要添加线程的时候,可以定义结构体的实例。

 

view sourceprint?01 //结构体   

02   struct RowCol   

03   {   

04   public int row;   

05   public int col;   

06   };   

07   

08 //定义方法   

09 public void Output(Object rc)   

10   {   

11   RowCol rowCol = (RowCol)rc;   

12   for (int i = 0; i < rowCol.row; i++)   

13   {   

14   for (int j = 0; j < rowCol.col; j++)   

15   Console.Write("{0} ", _char);   

16   Console.Write("\n");   

17   }   

18   }

 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat