当前位置:网站首页>C # cross thread refresh foreground UI
C # cross thread refresh foreground UI
2022-07-28 04:08:00 【ViperL1】
stay WinForm When programming in , If we calculate directly in the main process, we will get stuck UI Threads , In this case, the user experience will be very poor , However, if you operate directly in the background, you can't give users a direct feedback . How to achieve asynchronous computing + The front desk is refreshed , There are several methods for your reference .
One 、 Directly shield the inspection between threads
This is the most lazy way , because C# Will check the calling process between different threads , By default, this operation may cause unsafe phenomena , So it is forbidden , We can go to init Add a line of code to the function to remove the restriction .
Control.CheckForIllegalCrossThreadCalls = false;// Cancel the security check between threads Two 、 Use timer
The timer can allow specific code for a certain time , It's like polling , We can do background operation by placing code in the timer + Front desk update .
But it should be noted that if the timer touches Send too often Or it may cause the front desk to jam and even cannot move ( Trigger time is too short / Too many timers )
Timer tm = new Timer(); // Initialize the timer tm.Interval = 10; // Timer interval
tm.Tick += Tm_Tick; // Set the timer to trigger the event
tm.Start(); // Start timer private void Tm_Tick(object sender, EventArgs e)
{
// Code to execute
tm.Stop(); // Terminate timer
}3、 ... and 、 Use asynchronous
This method is the most convenient and safe method , It is highly recommended
new Task(new Action(() =>
{
this.Invoke(new Action(() =>
{
// Code to execute
}));
})).Start();边栏推荐
- What technical capabilities should a qualified software testing engineer have?
- Summary of static blog building tools
- 常用的弱网测试工具
- Selenium -- Web automated testing tool
- Error no matching function for call to 'std:: exception:: exception (const char [15])' problem solving
- un7.27:redis数据库常用命令。
- Construction and use of FTP server and NFS server
- Detailed explanation of string + memory function (C language)
- Recursion and non recursion are used to calculate the nth Fibonacci number respectively
- ftp服务器、nfs服务器的搭建和使用
猜你喜欢

Program life | test engineers only know a little? Seven shortcuts teach you to learn new technology quickly

企业数字化建设“三不五要”原则

It's amazing. This article completely explains the service interface testing

Fourier series

Leetcode58. Length of the last word

MATLB | location and constant volume IEEE30 node implementation of distributed energy

test case management tool

Machine learning 06: Decision Tree Learning

What technical capabilities should a qualified software testing engineer have?

How does MySQL ensure high availability
随机推荐
Kingbasees security guide of Jincang database -- 6.2. Configuration files related to authentication
巧用栈回溯,帮你快速定位问题
Data rich Computing: m.2 meets AI at the edge
【day03】流程控制语句
常用的弱网测试工具
H265/hevc noun explanation -- CTU, CTB, Cu, CB, Tu, PU, TB, Pb, LCU, slice, tile, chroma, luma, I frame, B frame, P frame
Difference between on, where and having
Recursion and non recursion are used to calculate the nth Fibonacci number respectively
Go结构体
虚拟机类加载机制
Dynamic programming - 416. Segmentation and subsets
Summary and interpretation of CONDA virtual environment
[day03] process control statement
Appnium--APP自动化测试工具
月薪28K学员 自动化测试经验分享
常用的接口测试工具
xml文件使用及解析
【无标题】
RT-Thread改变打印串口(在BSP的基础上添加其他功能)
Shell rental reptile