zoukankan      html  css  js  c++  java
  • 多线程传递参数的两种方式

    1 带参数的委托方法来传递参数

            static void Main(string[] args)

            {

                var d = new Data { Message = "中国" };

                //通过带参数的委托方法来传递参数 ParameterizedThreadStart

                var t2 = new Thread(ThreadMainWithParameters);

                t2.Start(d);

                System.Console.ReadLine();

            }

            static void ThreadMainWithParameters(object o)

            {

                Data d = (Data)o;

                Console.WriteLine("Running in a thread, received {0}", d.Message);

            }

    2 通过成员方法和带参数的构造函数传参数 

        class Program

        {

            static void Main(string[] args)

            {

                var obj = new MyThread("广东");

                var t3 = new Thread(obj.ThreadMain);

                t3.Start();

                System.Console.ReadLine();

            }

    }

        public class MyThread

        {

            private string data;

            public MyThread(string data)

            {

                this.data = data;

            }

            /// <summary>

            /// 线程方法

            /// </summary>

            public void ThreadMain()

            {

                Console.WriteLine("Running in a thread, data: {0}", data);

            }

        }

        public struct Data

        {

            public string Message;

        }

  • 相关阅读:
    python之os模块
    python之字符串
    python之爬虫(beautifulsoup)
    python之常见算法
    python之装饰器(类装饰器,函数装饰器)
    python之mock使用,基于unittest
    python之定时器
    python基础语法随记
    redis基础
    移动端页面开发(二)
  • 原文地址:https://www.cnblogs.com/shi5588/p/6130536.html
Copyright © 2011-2022 走看看