C# 获取线程类变量值问题

.Net技术 码拜 7年前 (2014-12-13) 832次浏览 0个评论
//启动线程
        private void button1_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            list.Add("192.168.9.14:8000:31214");
            list.Add("192.168.9.201:8000:31201");

            foreach (string strvalue in list)
            {
                string[] str = strvalue.Split(':');
                screenshotsthread = new ScreenshotsThread();
                screenshotsthread.setAddr(str[0].ToString());
                screenshotsthread.setPort(str[1].ToString());
                screenshotsthread.setId(str[2].ToString());
                screenshotsthread.start();
            }
        }

//获取线程变量质

  private void button2_Click_1(object sender, EventArgs e)
        {
            screenshotsthread = new ScreenshotsThread();
            textBox3.Text = screenshotsthread.Info;
            FuncWrite("实例类获取到值:" + screenshotsthread.Info);
        }

  //票队列处理线程
    class ScreenshotsThread
    {
        private String IpAddress = "";
        private string IPport = "";
        private String tid = "";
        Thread thread = null;
        static object _getTerminalObj = new object();//锁定对象
        byte[] buffer = new byte[1024];
        Socket socket;
        bool _state = true;
        string _info;
   
        //IP地址
        public void setAddr(String IpAddr)
        {
            IpAddress = IpAddr;
        }

       public bool state
        {
            get { return _state; }
            set { _state = value; }
        }

        public string Info
        {
            get { return _info; }
            set { _info = value; }
        }

        //端口号
        public void setPort(string Port)
        {
            IPport = Port;
        }
        //机器编码
        public void setId(String id)
        {
            tid = id;
        }
        public void start()
        {
            if (thread == null)
                thread = new Thread(run);
            thread.IsBackground = true;
            thread.Start();
        }

        public void run()
        {
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(IpAddress, int.Parse(IPport));
                socket.Receive(buffer);
            }
            catch (Exception ex)
            {
                throw new Exception("读取截屏据数据存在错误!");
            }
            socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket);
        }

        void ReceiveMessage(IAsyncResult ar)
        {
            try
            {
                var socket = ar.AsyncState as Socket;
                var length = socket.EndReceive(ar);
                byte[] reallData = new byte[length];
                Array.Copy(buffer, reallData, length);
                string str = System.Text.Encoding.UTF8.GetString(reallData);
                // FuncWrite(System.Text.Encoding.UTF8.GetString(reallData));
                if (str.IndexOf("确认要退出系统吗") > -1)
                {
                    Info = tid + ":" + str;
                }
                else if (str.Contains("请输入") && str.Contains("选择竞猜内容"))
                {
                    Info = tid + ":" + str;
                }
                else if (str.Contains("请输入") && str.Contains("内数字选择日期"))
                {
                    Info = tid + ":" + str;
                }
                else if (str.Contains("请输入") && str.Contains("数字选择日期"))
                {
                    Info = tid + ":" + str;

               }
                else if (str.Contains("没有第") && str.Contains("场比赛"))
                {
                    Info = tid + ":" + str;
                }
                else if (str.Contains("周") && str.Contains("没有赛事"))
                {
                    Info = tid + ":" + str;
                }
                else if (str.Contains("倍数最小为") && str.Contains("请重新输入"))
                {
                    Info = tid + ":" + str;
                   }
                else if (str.Contains("当前操作尚未完成") && str.Contains("是否确定要退出"))
                {
                    Info = tid + ":" + str;
                }
                else if (str.IndexOf("倍投范围为") > -1)
                {
                    Info = tid + ":" + str;
                }
                Thread.Sleep(800);
            }
            catch (Exception ex)
            {
                // FuncWrite(ex.Message);
            }
            finally
            {
                socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket);
            }
        }----

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# 获取线程类变量值问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!