码迷,mamicode.com
首页 > 编程语言 > 详细

C# 多线程参数的使用

时间:2014-07-05 18:01:32      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   art   

一个参数:

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

C# 多线程参数的使用

标签:style   blog   color   使用   os   art   

原文地址:http://www.cnblogs.com/bomb12138/p/3822828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!