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;
        private delegate void FlushClient();//代理
        //Thread thread = null;        public Frm_Main()
        {
            InitializeComponent();
                 }
        private void View1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {        }        private void Frm_Main_Load(object sender, EventArgs e)
        {   while (true)
               {
                    byte[] retdata = new byte[] { 0x55, 0xAA, 0x04, 0x00, 0x00, 0x00, 0x00, 0x48, 0x10, 0x5C, 0x79, 0x00, 0x00, 0x00, 0x31 };
                    //byte[] retdata1 = new byte[] { 0x55, 0xAA, 0x03, 0x00, 0x00, 0x00, 0x00, 0x48, 0x10, 0x32, 0x5B, 0x00, 0x00, 0x00, 0xE8 };
                    byte[] shunshibuff = new byte[4];
                    byte[] liusubuff = new byte[4];
                    byte[] leijiliangbuff = new byte[4];                    shunshibuff[3] = retdata[3];
                    shunshibuff[2] = retdata[4];
                    shunshibuff[1] = retdata[5];
                    shunshibuff[0] = retdata[6];                    shun_liuliang = BitConverter.ToSingle(shunshibuff, 0).ToString("0.00");
                    liusubuff[3] = retdata[7];
                    liusubuff[2] = retdata[8];
                    liusubuff[1] = retdata[9];
                    liusubuff[0] = retdata[10];
                    shun_liusu = BitConverter.ToSingle(liusubuff, 0).ToString("0.00");                    leijiliangbuff[3] = retdata[11];
                    leijiliangbuff[2] = retdata[12];
                    leijiliangbuff[1] = retdata[13];
                    leijiliangbuff[0] = retdata[14];
                    leiji = BitConverter.ToUInt32(leijiliangbuff, 0).ToString();
                    if (retdata[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 (retdata[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 (retdata[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 (retdata[2] == 04)
                    {
                        this.label4.Text =  shun_liuliang + "\r\n";
                       label1.Refresh();
                        label4.Text += leiji + "\r\n";
                        label1.Refresh();
                       label4.Text += shun_liusu + "\r\n";
                       Application.DoEvents();
                       
                     }
                    
              }
         }
   
        public void printData(string str1)
        {
            //创建文件流对象实例
            string path = "d:/logs/";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = path + DateTime.Today.ToString("yyyyMMdd") + "RecDataLog.txt";
         FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
            try
            {
                sw.WriteLine(str1);
            }
            catch (Exception ex)
            {
                printData(ex.Message);
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }
      
        private void label1_Click(object sender, EventArgs e)
        {}
我希望能让4个label根据输入的不同不断地刷新,但是这个程序不出现窗口,一直在循环,网上说需要线程控制,但是参数调用时,我不知道该怎么改相应的地方,希望大家能多帮帮我,我刚开始学习C#,不是太懂。C#线程循环label控件Path

解决方案 »

  1.   

    不要写在FormLoad中,或者在FormLoad前面加一句
    this.Show();
      

  2.   

    msdn这个牛逼知识库都不会去看http://msdn.microsoft.com/zh-cn/library/ms171728%28v=VS.80%29.aspx
      

  3.   

     楼主可以安装个离线的msdn library,先问msdn再问csdn
      

  4.   

    Control.CheckForIllegalCrossThreadCalls = false;
      

  5.   

    因为不是一个线程里的,所以你必须通过当前Form的委托来处理。。
      

  6.   

    CheckForIllegalCrossThreadCalls 或委托调用建议使用 委托调用
      

  7.   

    参考这个文章中的解决方案:http://www.cnblogs.com/zhili/archive/2013/05/10/APM.html
      

  8.   

    这个是治标不治本 微软是不提倡跨线程访问的 可以用委托来实现。
    //主线程
     public delegate void UpdateMessage(string mes);
     public void UpdatePortMessage(string mes)
            {
                this.txtMessage.AppendText(string.Format("Connected {0}. succeed", mes) + "\r\n");
                this.txtClientIP.Text = mes;
            }
    // 线程1
      UpdateMessage upPort = new UpdateMessage(UpdatePortMessage);
                            this.BeginInvoke(upPort, point);
      

  9.   

    form是主线程,所有的控件都是主线程的东西,你其他的线程当然不能访问了,简单点的实现就是,你放一些全局变量,然后把控件的值放在这些变量里面,其他的线程访问这些全局变量。