线程间操作无效: 从不是创建控件“label4”的线程访问它
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#,不是太懂。