c# 多线程 调用带参数函数

来源:互联网 发布:日系淘宝店铺推荐 编辑:程序博客网 时间:2024/05/01 20:47

  1.   线程操作主要用到Thread类,他是定义在System.Threading.dll下。使用时需要添加这一个引用。该类提供给我们四个重载的构造函数(以下引自msdn)。   
  2.   
  3. Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。    
  4. Thread (ThreadStart) 初始化 Thread 类的新实例。   
  5. 由 .NET Compact Framework 支持。   
  6.    
  7. Thread (ParameterizedThreadStart, Int32) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。    
  8. Thread (ThreadStart, Int32) 初始化 Thread 类的新实例,指定线程的最大堆栈大小。   
  9. 由 .NET Compact Framework 支持。  
  10.    
  11.   
  12.   
  13.   我们如果定义不带参数的线程,可以用ThreadStart,带一个参数的用ParameterizedThreadStart。带多个参数的用另外的方法,下面逐一讲述。  
  14.   
  15. 一、不带参数的  
  16.   
  17. using System;  
  18. using System.Collections.Generic;  
  19. using System.Text;  
  20. using System.Threading;  
  21.   
  22. namespace AAAAAA  
  23. {  
  24.   class AAA  
  25.   {  
  26.   public static void Main()  
  27.   {  
  28.   Thread t = new Thread(new ThreadStart(A));  
  29.   t.Start();  
  30.   
  31.   Console.Read();  
  32.   }  
  33.   
  34.   private static void A()  
  35.   {  
  36.   Console.WriteLine("Method A!");  
  37.   }  
  38.   }  
  39. }  
  40.    
  41.   
  42.   结果显示Method A!  
  43.   
  44. 二、带一个参数的  
  45.   
  46.   由于ParameterizedThreadStart要求参数类型必须为object,所以定义的方法B形参类型必须为object。  
  47.   
  48. using System;  
  49. using System.Collections.Generic;  
  50. using System.Text;  
  51. using System.Threading;  
  52.   
  53. namespace AAAAAA  
  54. {  
  55.   class AAA  
  56.   {  
  57.   public static void Main()  
  58.   {    
  59.   Thread t = new Thread(new ParameterizedThreadStart(B));  
  60.   t.Start("B");  
  61.   
  62.   Console.Read();  
  63.   }  
  64.   
  65.   private static void B(object obj)  
  66.   {  
  67.   Console.WriteLine("Method {0}!",obj.ToString ());  
  68.   
  69.   }  
  70.   }  
  71. }  
  72.    
  73.   
  74.  结果显示Method B!   
  75.   
  76. 三、带多个参数的  
  77.   
  78.   由于Thread默认只提供了这两种构造函数,如果需要传递多个参数,我们可以自己将参数作为类的属性。定义类的对象时候实例化这个属性,然后进行操作。  
  79.   
  80. using System;  
  81. using System.Collections.Generic;  
  82. using System.Text;  
  83. using System.Threading;  
  84.   
  85. namespace AAAAAA  
  86. {  
  87.   class AAA  
  88.   {  
  89.   public static void Main()  
  90.   {  
  91.   My m = new My();  
  92.   m.x = 2;  
  93.   m.y = 3;  
  94.   
  95.   Thread t = new Thread(new ThreadStart(m.C));  
  96.   t.Start();  
  97.   
  98.   Console.Read();  
  99.   }  
  100.   }  
  101.   
  102.   class My  
  103.   {  
  104.   public int x, y;  
  105.   
  106.   public void C()  
  107.   {  
  108.   Console.WriteLine("x={0},y={1}"this.x, this.y);  
  109.   }  
  110.   }  
  111. }  
  112.    
  113.   
  114.   结果显示x=2,y=3   
  115.   
  116. 四、利用结构体给参数传值。  
  117.   
  118. 定义公用的public struct,里面可以定义自己需要的参数,然后在需要添加线程的时候,可以定义结构体的实例。  
  119.   
  120. //结构体  
  121.   struct RowCol  
  122.   {  
  123.   public int row;  
  124.   public int col;  
  125.   };  
  126.   
  127. //定义方法  
  128.  public void Output(Object rc)  
  129.   {  
  130.   RowCol rowCol = (RowCol)rc;  
  131.   for (int i = 0; i < rowCol.row; i++)  
  132.   {  
  133.   for (int j = 0; j < rowCol.col; j++)  
  134.   Console.Write("{0} ", _char);  
  135.   Console.Write("\n");  
  136.   }  
  137.   }   
0 0
原创粉丝点击