c#报错 :System . Invalid Operation Exception:“线程间操作无效: 从不是创建控件的线程访问它
一、问题来源跨线程操作时会报错:System.InvalidOperationException:“线程间操作无效: 从不是创建控件的线程访问它。二、问题代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using Sy
文章共475字 · 阅读需要大约2分钟
一键AI生成摘要,助你高效阅读
问答
·
一、问题来源
跨线程操作时会报错:System.InvalidOperationException:“线程间操作无效: 从不是创建控件的线程访问它。
二、问题代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
//using MSExcel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
List<string> savedData;
public string stra = null;
public string strs = null;
public string stra1 = null;
public string stra2 = null;
String[] recevedData;
string path;
public string filename;
Thread th;
Thread thWriteExcel;
Socket socketSend;
bool isCollecting = false;
string datadirection = null;
int numData; //收到的数据个数;
public Form1()
{
InitializeComponent();
numData = 0;
savedData = new List<string>();
}
private void label6_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.ShowDialog();
}
//采集
private void label5_Click(object sender, EventArgs e)
{
try
{
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //初始化
IPAddress ip = IPAddress.Parse("192.168.199.1");
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(8080));
socketSend.Connect(point);
MessageBox.Show("连接成功!");
th = new Thread(ReceiveData);
th.IsBackground = true;
th.Start();
isCollecting = true;
}
catch (SocketException ex)
{
return;
}
label5.Text = "采集中";
this.label5.Image = global::WindowsFormsApp1.Properties.Resources.anniu_jh;
}
//接收数据
public void ReceiveData()
{
while (isCollecting)
{
try
{
byte[] receiveMessage = new byte[1024 * 1024 * 2];
//接收的字节数
int receiveNumber = 0;
receiveNumber = socketSend.Receive(receiveMessage);
// MessageBox.Show(receiveNumber.ToString());
if (receiveNumber == 0)
{
break;
}
//字节转换成字符串
string str = Encoding.UTF8.GetString(receiveMessage, 0, receiveNumber);
//打印到窗体
recevedData = str.Split(',');
printMessage();
strs = SubData(0) + ":\r\n";
stra = null;
stra1 = null;
stra2 = null;
numData++;
}
catch (SocketException e)
{
MessageBox.Show(e.ToString());
}
}
}
//数据输出
public string SubData(int p)
{
return recevedData[p];
}
private void printMessage()
{
label7.Text = SubData(1);
}
private void label4_Click(object sender, EventArgs e)
{
if (isCollecting)
{
th.Abort();
isCollecting = false;
socketSend.Shutdown(SocketShutdown.Both);
socketSend.Close();
MessageBox.Show("停止采集");
label4.Text = "开始采集";
this.label4.Image = global::WindowsFormsApp1.Properties.Resources.anniu;
}
}
}
}
三、问题截图
四、解决方案
加入 Control.CheckForIllegalCrossThreadCalls = false; //加载时 取消跨线程检查
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; //加载时 取消跨线程检查
numData = 0;
savedData = new List<string>();
}
更多推荐
已为社区贡献1条内容
所有评论(0)