当前位置:网站首页>解决C#跨线程调用窗体控件的问题
解决C#跨线程调用窗体控件的问题
2022-07-26 08:30:00 【luobeihai】
遇到问题
在用C#写一个窗体应用程序的时候,调试的时候突然报错,说我的线程操作无效,不是从创建控件的线程去访问控件。
网上查了下出现这个问题的原因是:
C#只有主线程才能访问控件。
从 .NET Framework 2.0 类库开始,.net框架就对于 winform 中采用多线程调用窗体控件进行了安全性检测,就是说我从另外一个不是主线程的线程去调用窗体控件的话,就会出现异常。
解决方法
解决跨线程间调用的方法有很多,这里介绍两种。
1. 直接关闭跨线程访问控件的安全检查
关闭安全检查,我们只要在窗体函数中加入下面一句代码即可:
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; // 这句代码就是关闭了安全检查
}
这种方法是最简单的,直接关闭了跨线程间访问控件的安全检查,那么就可以允许你在其他各个子线程乱改控件的属性,这样的访问是不可控的,所以这种方法不推荐使用。
2. 通过委托调用
C#的委托调用,类似C语言中的函数指针作为函数参数一样。
C#提供了delegate和invoke来从其他线程中调用控件。
简化的使用方法如下,就是直接在需要跨线程访问控件的那部分代码,写在 delegate 括起来的里面即可。
this.Invoke(new EventHandler (delegate
{
// 这里面就是访问控件相关的代码了
label1.Text = "hello";
}));
比如,我前面一开始遇到的,就是我在一个网络数据接收的线程,把接收的数据写到接收的文本框里面,不加 Invoke 的话就会报错,加了就不会,如下代码:
private void ReceiveCallback(IAsyncResult AR)
{
// Check how much bytes are recieved and call EndRecieve to finalize handshake
this.Invoke(new EventHandler(delegate
{
try
{
int recieved = tcpClient.EndReceive(AR);
if (recieved <= 0)
return;
string message = Encoding.GetEncoding("GBK").GetString(recieveBuffer, 0, recieved); // 只将接收到的数据进行转化
// 这里就是跨线程访问了tbRecv文本框控件,这里实际上就是想把接收到的数据添加到文本框上显示出来
tbRecv.AppendText($"[{
DateTime.Now.ToString("yyy-MM-dd HH.mm.ss.fff")}] {
message}\r\n");
// Start receiving again
tcpClient.BeginReceive(recieveBuffer, 0, recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch (SocketException ex)
{
MessageBox.Show(ex.ToString());
}
}));
}
边栏推荐
- 2022-024arts: Longest valid bracket
- 基于C语言的内存管理-动态分区分配方式模拟
- B title: razlika priority queue approach
- Please tell me if there is any way to increase the write out rate when the Flink SQL client is in the sink table. When synchronizing through sink table
- import error: ‘Icon‘ is not exported from ‘antd‘. Import icon error
- 六、品达通用权限系统__pd-tools-log
- Spark scheduling analysis
- Awk operation
- awk作业
- QSS add resource file of QT
猜你喜欢
苹果强硬新规:用第三方支付也要抽成,开发者亏大了!
Daily Note (11) -- word formula input arbitrary matrix
Special lecture 2 dynamic planning learning experience (should be updated for a long time)
Winter vacation homework & Stamp cutting
22-07-14 personal training match 2 competition experience
2022-7-5 personal qualifying 2 competition experience
Basic configuration of BGP
[C language] programmer's basic skill method - "creation and destruction of function stack frames"
Kotlin data type
OSPF summary
随机推荐
The data read by Flink Oracle CDC is always null. Do you know
Error handling response: Error: Syntax error, unrecognized expression: .c-container /deep/ .c-contai
内存管理-动态分区分配方式模拟
Kotlin variables and constants
Flutter custom player progress bar
Run file command
Leetcode and query question summary
Day 3 homework
BGP -- Border Gateway Protocol
Flutter text is left aligned with no blank space in the middle
基于Raft共识协议的KV数据库
23.8 using the applicationrunner or commandlinerunner to implement applicationrunner and commandlinerunner
6、 Pinda general permission system__ pd-tools-log
Analysis on the query method and efficiency of Oracle about date type
Kotlin中room数据库的使用
Take out brother is the biggest support in this society
Flutter distribution
为什么要在时钟输出上预留电容的工位?
第四天作业
Excel delete blank lines