zoukankan      html  css  js  c++  java
  • 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"); 
         } 
       } 
  • 相关阅读:
    维护win10注册表
    win10操作技巧
    无处不网络
    事件驱动编程思想
    流程控制之if...else
    python----GIL的概念
    并发与同步异步的概念
    实现并发join的方法
    线程的调用
    三元运算符
  • 原文地址:https://www.cnblogs.com/xietianjiao/p/11929059.html
Copyright © 2011-2022 走看看