C#跨线程访问(二)----thread参数、回调传参数

一、单个参数(封箱也可实现多参数)

class B
  { 
     public static void Main() 
     {  
        Thread t = new Thread(new ParameterizedThreadStart(Test)); 
        t.Start("a"); 
 
        Console.Read(); 
      } 
 
      private static void Test(object obj) 
      { 
        Console.WriteLine("带一个参数 {0}!",obj.ToString ());  
      } 
   } 
二、多个参数

 class Test2
  { 
     public static void Main() 
     { 
        MyParam m new MyParam(); 
        m.x = 6; 
        m.y = 9; 
 
        Thread t new Thread(new ThreadStart(m.Test)); 
        t.Start(); 
 
        Console.Read(); 
      } 
  } 
  
  class MyParam  
  { 
     public int x, y; 
 
     public void Test() 
     { 
         Console.WriteLine("x={0},y={1}"this.x, this.y); 
     } 
   } 

三、回调函数传参

class C
  { 
     public static void Main() 
     { 
        MyParam m new MyParam(); 
        m.x = 6; 
        m.y = 9; 
        m.callBack = ThreadCallBack;
 
        Thread t new Thread(new ThreadStart(m.Test)); 
        t.Start(); 
 
        Console.Read(); 
     } 
  } 

  private void ThreadCallBack(string msg)
  {
     Console.WriteLine("CallBack:" + msg);  
  }
 
  private delegate void ThreadCallBackDelegate(string msg);

  class MyParam  
  { 
     public int x, y; 
     public ThreadCallBackDelegate callBack;
 
     public void Test() 
     { 
        callBack("x=6,y=9"); 
     } 
   } 

 

posted @ 2019-11-25 17:13  卖雨伞的小男孩  阅读(1545)  评论(0编辑  收藏  举报