欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

子线程传值到界面显示C#

程序员文章站 2022-03-04 12:33:45
...
delegate void ShowDataCallBack(string StringData);
 int i = 0;
        public void Data(string message)
        {
            if (label1.InvokeRequired)
            {
                ShowDataCallBack callBack = new ShowDataCallBack(Data);
                label1.Invoke(callBack, new object[] { message + "   " + i.ToString() });
                i = i + 1;              
            }
            else
            {

                this.label1.Text = message + i.ToString();
            }
        }

 

private void Form1_Load(object sender, EventArgs e) { //打开软件开始接受条码数据 Thread thread1 = new Thread(new ThreadStart(GetBarcodeInfo)); thread1.Start(); // GetJsonData(); }

 

public void GetBarcodeInfo()
        {
            string receiveString = null;
            byte[] receiveData = null;


            //实例化一个远程端点,IP和端口可以随意指定,等调用client.Receive(ref remotePoint)时会将该端点改成真正发送端端点 
            IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
            int port = 11000;

            while (true)
            {
                UdpClient client = null;
                client = new UdpClient(port);
                receiveData = client.Receive(ref remotePoint);//接收数据 
                receiveString = Encoding.Default.GetString(receiveData);
                Data(receiveString);
                Thread.Sleep(1000);
                Data("");
                client.Close();//关闭连接 
            }
        }