当前位置:网站首页>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();边栏推荐
- Regression - linear regression
- Analysis of static broadcast transmission process
- 程序人生 | 测试工程师还只会点点点?7个捷径教给你快速学习新技术...
- H265/hevc noun explanation -- CTU, CTB, Cu, CB, Tu, PU, TB, Pb, LCU, slice, tile, chroma, luma, I frame, B frame, P frame
- Machine learning 07: Bayesian learning
- Detailed explanation of pl/sql parameters ("box model")
- Monotonic stack - 739. Daily temperature
- 7/27(板子)染色法判定二分图+求组合数(递推公式)
- Notes on writing questions in sword finger offer
- 2022.7.13-----leetcode.735
猜你喜欢

Is there a bonus period for robot engineering

Kotlin——函数

Machine learning 07: Bayesian learning

How does MySQL ensure high availability

pl/sql之各参数详解(“箱子模型“)

STC timer is abnormal (how to modify the initial value, the timing time is 100ms)

Developing rc522 module based on c8t6 chip to realize breathing lamp

servlet使用

Simple and easy-to-use performance testing tools recommended

Classification cluster analysis
随机推荐
How does MySQL ensure high availability
Regression - linear regression
Machine learning 06: Decision Tree Learning
Move notice!
Classification cluster analysis
Leetcode58. 最后一个单词的长度
Recursion and non recursion are used to calculate the nth Fibonacci number respectively
7/27 (board) dyeing method to determine bipartite graph + find combination number (recursive formula)
Un7.27: how to successfully build a ruoyi framework project in idea?
测试用例管理工具
Developing rc522 module based on c8t6 chip to realize breathing lamp
Filters, interceptors, listeners
Program life | test engineers only know a little? Seven shortcuts teach you to learn new technology quickly
[untitled]
【无标题】
Experience sharing of automatic test for students with monthly salary of 28K
[Luogu p4590] garden party (DP set DP)
LeetCode 0140. 单词拆分 II
Analysis of static broadcast transmission process
conda虚拟环境总结与解读