using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Runtime.InteropServices;
namespace GetCode
{
    public partial class Frm_Main : Form
    { byte[] recvBytes = new byte[1024];
        byte[] dataBuff = new byte[7];
        static string shun_liusu;
        static string shun_liuliang;
        static string leiji;
        Socket sockC; 
        TimeOutSock timeSock = new TimeOutSock();
        Thread thrCli;
        public Frm_Main()
        {
            InitializeComponent();
            thrCli = new Thread(new ThreadStart(GuidProc));
            thrCli.Start();        }
        private void View1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }
        private delegate void SetLabelText(string value);
        private void SetLabel1(string value)
        {
            if (this.InvokeRequired)
            {
               SetLabelText d = new SetLabelText(SetLabel1); 
           object arg = (shun_liuliang);
           this.Invoke(d, arg);
            }
            else
            {
                if (recvBytes[2] == 01)
                {
                    this.label1.Text = shun_liuliang + "\r\n";
                    this.label1.Text += leiji + "\r\n";
                    this.label1.Text += shun_liusu + "\r\n";
                }
                else if (recvBytes[2] == 02)
                {
                    label2.Text = shun_liuliang + "\r\n";
                    label2.Text += leiji + "\r\n";
                    label2.Text += shun_liusu + "\r\n";
                }
                else if (recvBytes[2] == 03)
                {
                    label3.Text = shun_liuliang + "\r\n";
                    label3.Text += leiji + "\r\n";
                    label3.Text += shun_liusu + "\r\n";
                }
                else if (recvBytes[2] == 04)
                {
                    this.label4.Text = shun_liuliang + "\r\n";
                    label4.Text += leiji + "\r\n";
                    label4.Text += shun_liusu + "\r\n";                }            }
        }
        private void GuidProc()
        {
            while (true)
            {
                SetLabel1(Guid.NewGuid().ToString());
       }
        }
        private void Frm_Main_Load(object sender, EventArgs e)
        {
         
            try
         {
                int port = 10001;
                IPAddress ip = IPAddress.Parse("192.168.1.239");
                IPEndPoint ipe = new IPEndPoint(ip, port);
                try
                {
                    sockC = timeSock.Connect(ipe, 3000);                }
                catch (Exception ex)
                {
                   // MessageBox.Show(ex.Message);
                }
              
                    int bytes = 0;                    string time = DateTime.Now.ToString();                    try
                    {
                        if (sockC != null && sockC.Connected && sockC.Poll(-1, SelectMode.SelectRead))
                        {
                            Thread.Sleep(100);
                            bytes = sockC.Receive(recvBytes, 15, 0);
                        }
                    }
                    catch (SocketException b)
                    {
                    }
                    byte[] shunshibuff = new byte[4];
                    byte[] liusubuff = new byte[4];
                    byte[] leijiliangbuff = new byte[4];                    shunshibuff[3] = recvBytes[3];
                    shunshibuff[2] = recvBytes[4];
                    shunshibuff[1] = recvBytes[5];
                    shunshibuff[0] = recvBytes[6];                    shun_liuliang = BitConverter.ToSingle(shunshibuff, 0).ToString("0.00");
                    liusubuff[3] = recvBytes[7];
                    liusubuff[2] = recvBytes[8];
                    liusubuff[1] = recvBytes[9];
                    liusubuff[0] = recvBytes[10];
                    shun_liusu = BitConverter.ToSingle(liusubuff, 0).ToString("0.00");                    leijiliangbuff[3] = recvBytes[11];
                    leijiliangbuff[2] = recvBytes[12];
                    leijiliangbuff[1] = recvBytes[13];
                    leijiliangbuff[0] = recvBytes[14];
                    leiji = BitConverter.ToUInt32(leijiliangbuff, 0).ToString();
                 catch (ArgumentNullException ex)
            {
               // printData(ex.Message);
            }
           
        }           
        public void procMsg( )
        {
            byte[] shunshibuff = new byte[4];
            byte[] liusubuff = new byte[4];
            byte[] leijiliangbuff = new byte[4];            shunshibuff[3] = recvBytes[3];
            shunshibuff[2] = recvBytes[4];
            shunshibuff[1] = recvBytes[5];
            shunshibuff[0] = recvBytes[6];            shun_liuliang = BitConverter.ToSingle(shunshibuff, 0).ToString("0.00");
            liusubuff[3] = recvBytes[7];
            liusubuff[2] = recvBytes[8];
            liusubuff[1] = recvBytes[9];
            liusubuff[0] = recvBytes[10];
            shun_liusu = BitConverter.ToSingle(liusubuff, 0).ToString("0.00");            leijiliangbuff[3] = recvBytes[11];
            leijiliangbuff[2] = recvBytes[12];
            leijiliangbuff[1] = recvBytes[13];
            leijiliangbuff[0] = recvBytes[14];
            leiji = BitConverter.ToUInt32(leijiliangbuff, 0).ToString();
            if (recvBytes[2] == 01)
            {
                label1.Text = shun_liuliang + "\r\n";
                label1.Refresh();
                label1.Text += leiji + "\r\n";
                label1.Refresh();
                label1.Text += shun_liusu + "\r\n";
                label1.Refresh();
            }
            else if (recvBytes[2] == 02)
            {
                label2.Text = shun_liuliang + "\r\n";
                Application.DoEvents();
                label2.Text += leiji + "\r\n";
                Application.DoEvents();
                label2.Text += shun_liusu + "\r\n";
                Application.DoEvents();
            }
            else if (recvBytes[2] == 03)
            {
                label3.Text = shun_liuliang + "\r\n";
                Application.DoEvents();
                label3.Text += leiji + "\r\n";
                Application.DoEvents();
                label3.Text += shun_liusu + "\r\n";
                Application.DoEvents();
            }
            else if (recvBytes[2] == 04)
            {
               label4.Text = shun_liuliang + "\r\n";
                label4.Text += leiji + "\r\n";
                label4.Text += shun_liusu + "\r\n";
                Application.DoEvents();
            }
        }
     
}
label循环显示线程调用线程异常