当前位置:网站首页>Multithreaded learning 2- call control
Multithreaded learning 2- call control
2022-06-25 23:20:00 【WhyLearnCode】
Call control
1 Message mechanism to calling control
Windows GUI The program is based on the message mechanism , A main thread maintains a message pump . This message pumps windows The program never stops . If you operate from another thread windows Controls on forms , It will compete with the main thread , Cause unexpected results , Even deadlock. . therefore windows GUI There is a rule in programming , That is, the data of the control can only be operated by the thread that creates the control , Otherwise, it may produce unpredictable results .
because Windows Form controls are not thread safe in nature . Therefore, if there are two or more threads operating the state of a control appropriately (set value), May force the control into an inconsistent state . There may also be other thread related bug, Including contention and deadlock . So when you run an application in the debugger , If a thread other than the one that created the control attempts to call the control , The debugger raises a InvalidOperationException.
Common mistakes :
Invalid inter thread operation : Never create controls “xxxx” Thread access it
Invalid parameter
2 Three solutions
2.1 Turn off anomaly detection
Turning off exception detection is a direct way to avoid throwing exceptions , Is the simplest of the three methods . After testing, it is found that this method avoids exception throwing , But it can't guarantee the correctness of the program running results
Control.CheckForIllegalCrossThreadCalls = false;
2.2 Through delegation security call
private TextBox _TextBox;
private string _Value;
delegate void SetTextCallback(TextBox TxtBox, String Value);
public Form5()
{
InitializeComponent();
//Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(ThreadProcSafe));
thread.Start();
}
private void ThreadProcSafe()
{
for (int i = 0; i < 100; i++)
{
//this.SetText($"This text was set safely.{i}");
SetText2(this.textBox1,$"textbox1+{i}");
SetText2(this.textBox2, $"textbox2+{i}");
Thread.Sleep(100);
}
}
public void SetText(TextBox TxtBox, String Value)
{
_TextBox = TxtBox;
_Value = Value;
_TextBox.Text = _Value;
_TextBox.Refresh();
}
public void SetText2(TextBox TxtBox, String Value)
{
_TextBox = TxtBox;
_Value = Value;
if (_TextBox.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText2);
this.Invoke(d, new object[] {
_TextBox, Value });
}
else
{
_TextBox.Text = Value;
}
}
}
2.3 utilize BackgroundWorker Control
3 Reference resources
边栏推荐
- Oracle - data query
- 小程序绘制一个简单的饼图
- What is 5g? What can 5g do? What will 5g bring in the future?
- ES6 --- 数值扩展、对象拓展
- 干货丨产品的可行性分析要从哪几个方面入手?
- Common MySQL database functions and queries
- Mysql database index
- UE4\UE5 蓝图节点Delay与Retriggerable Delay的使用与区别
- [modulebuilder] GP service realizes the intersection selection of two layers in SDE
- 最近准备翻译外国优质文章
猜你喜欢
Wpewebkit debugging MSE playback
Circuit module analysis exercise 5 (power supply)
Why absolute positioning overlaps
Circuit module analysis exercise 6 (switch)
oracle -- 表操作
【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域
ES6-- 模板字符串、对象的简化写法、箭头函数
ES6 learning -- let
How to solve the problem of SQL?
Pit resolution encountered using East OCR (compile LAMS)
随机推荐
Unity的Ping类使用
What is 5g? What can 5g do? What will 5g bring in the future?
The applet draws a simple pie chart
Unity的Ping類使用
论文笔记: 多标签学习 MSWL
pdm导入vscode的实现方式
MySQL queries data by day, week, month, quarter and year
Circuit module analysis exercise 5 (power supply)
The Ping class of unity uses
2、一个向量乘它的转置,其几何意义是什么?
Some points to pay attention to when closing mongodb services (as well as related commands when opening)
【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域
Sword finger offer 46 Translate numbers to strings (DP)
ES6 const constants and array deconstruction
Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert
STM32 development board + smart cloud aiot+ home monitoring and control system
Record the learning record of the exists keyword once
What should it personnel over 35 years old do if they are laid off by the company one day?
ES6 learning -- let
Baidu: in 2022, the top ten hot spots will rise and the profession will be released. There is no suspense about the first place!