线程间操作无效: 从不是创建控件“label4”的线程访问它

shixiangyan0522 2013-08-02 09:15:26
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#,不是太懂。
...全文
1150 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
人生导师 2013-08-07
  • 打赏
  • 举报
回复
参考这个文章中的解决方案:http://www.cnblogs.com/zhili/archive/2013/05/10/APM.html
CqCoder 2013-08-07
  • 打赏
  • 举报
回复
CheckForIllegalCrossThreadCalls 或委托调用 建议使用 委托调用
  • 打赏
  • 举报
回复
跨线程操作控件用Invoke()
屌絲來襲 2013-08-07
  • 打赏
  • 举报
回复
沒看內容,看到標題我只能說,跨線程操作控件請使用委託
卧_槽 2013-08-07
  • 打赏
  • 举报
回复
需要一个委托
石灰石石英石 2013-08-07
  • 打赏
  • 举报
回复
form是主线程,所有的控件都是主线程的东西,你其他的线程当然不能访问了,简单点的实现就是,你放一些全局变量,然后把控件的值放在这些变量里面,其他的线程访问这些全局变量。
vssvss 2013-08-07
  • 打赏
  • 举报
回复
引用 6 楼 yuanqn 的回复:
Control.CheckForIllegalCrossThreadCalls = false;
这个是治标不治本 微软是不提倡跨线程访问的 可以用委托来实现。


//主线程
 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);
taotao_live 2013-08-05
  • 打赏
  • 举报
回复
因为不是一个线程里的,所以你必须通过当前Form的委托来处理。。
yuanqn 2013-08-05
  • 打赏
  • 举报
回复
Control.CheckForIllegalCrossThreadCalls = false;
shixiangyan0522 2013-08-02
  • 打赏
  • 举报
回复
this.show()不行。
智商余额不足 2013-08-02
  • 打赏
  • 举报
回复
楼主可以安装个离线的msdn library,先问msdn再问csdn
智商余额不足 2013-08-02
  • 打赏
  • 举报
回复
threenewbee 2013-08-02
  • 打赏
  • 举报
回复
不要写在FormLoad中,或者在FormLoad前面加一句 this.Show();

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧