首页 > 代码库 > C# 多线程参数的使用
C# 多线程参数的使用
一个参数:
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); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。