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"); 
         } 
       } 
  • 相关阅读:
    c语言指针讲解第一节初识指针
    linux的的一些入门常识
    sql手注的思路
    mysql主从备份配置
    CentOS 6.5 nginx+tomcat+ssl配置
    mysql 5.7.18安装教程
    minIO分布式集群搭建+nginx负载均衡
    Linux常用命令
    使用python连接mysql数据库——pymysql模块的使用
    with与上下文管理器
  • 原文地址:https://www.cnblogs.com/xietianjiao/p/11929059.html
Copyright © 2011-2022 走看看