zoukankan      html  css  js  c++  java
  • 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); }
  • 相关阅读:
    12.3
    团队项目第一阶段冲刺第一天
    4.22
    4.21 re重要功能
    12.1
    12.2
    4.17
    4.16
    css设置子元素相对于父元素保持位置不变(含有滚动条的父元素)
    git操作和npm操作清单
  • 原文地址:https://www.cnblogs.com/bomb12138/p/3822828.html
Copyright © 2011-2022 走看看