• C#中给线程传值并启动


    方法1:

    ParameterizedThreadStart 委托+Thread.Start 方法 (Object)

            private void btnLogin_Click(object sender, RoutedEventArgs e)
            {
                ////Start the update check procedure.
                string ssid = this.txtSSID.Text;
                string username = this.txtName.Text;
                string password = this.txtPassword.Text;
    //ParameterizedThreadStart 委托
                Thread loginThread = new Thread(new ParameterizedThreadStart(toLogin));
    //Thread.Start 方法 (Object)
                loginThread.Start(ssid + "," + username + "," + password);
            }
    
            private void toLogin(object parameters)
            {
                try
                {
                    string p = parameters as string;
                    string s = ",";
                    string[] name = p.Split(s.ToCharArray());
                    string ssid = name[0];
                    string username = name[1];
                    string password = name[2];
                    int errorCode = roaming_instance.Login(ssid, username, password);
                    Debug.WriteLine("login error code:" + errorCode);
                    if (AicentErrorCode.AICENT_OK == errorCode)
                    {
                        //login ok
                        //add your code
                    }
                    else
                    {
                        //failed to login
                        //add your code
                    }
                }
                catch (System.Exception ex)
                {
                    Debug.WriteLine("Assert failed as login error code not match:" + ex);
                }
            }

    方法2:

    ThreadStart 委托+实例对象方法

    using System;
    using System.Threading;
    
    class Test
    {
        static void Main() 
        {
            // To start a thread using an instance method for the thread 
            // procedure, use the instance variable and method name when 
            // you create the ThreadStart delegate. Beginning in version
            // 2.0 of the .NET Framework, the explicit delegate is not
            // required.
            //
            Work w = new Work();
            w.Data = 42;
    //ThreadStart 委托
            threadDelegate = new ThreadStart(w.DoMoreWork);
            newThread = new Thread(threadDelegate);
            newThread.Start();
        }
    }
    
    class Work 
    {
        public int Data;
    //实例对象方法
        public void DoMoreWork() 
        {
            Console.WriteLine("Instance thread procedure. Data={0}", Data); 
        }
    }
    
    /* This code example produces the following output (the order 
       of the lines might vary):
    
    Instance thread procedure. Data=42
     */
    




  • 相关阅读:
    Docker集群实验环境布署--swarm【4 管理组件--manager】
    Docker集群实验环境布署--swarm【3 注册服务监控与自动发现组件--consul】
    Docker集群实验环境布署--swarm【2 搭建本地镜像仓库】
    Docker集群实验环境布署--swarm【1 架构说明】
    zabbix 布署实践【7 H3C网络设备监控模版制作思路】
    zabbix 布署实践【6 使用微信公众号-消息模版推送告警】
    zabbix 布署实践【5 使用邮箱SMTP SSL推送告警邮件】
    zabbix 布署实践【4 服务器自动探索发现,并且自动关联模版】
    openstack私有云布署实践【19 通过python客户端 创建实例VM指定IP地址】
    openstack私有云布署实践【18 修改实例DHCP服务的DNS IP】
  • 原文地址:https://www.cnblogs.com/fifa0329/p/4536661.html
Copyright © 2020-2023  润新知