C# 多线程参数的使用

时间:2014-07-05 18:01:32   收藏:0   阅读:140

一个参数:

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); }

 

 

C# 多线程参数的使用,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!