In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to solve the error problem of multi-threaded update interface in C#. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Because of a threaded program, if the call to a function is blocked, it will affect the update of the interface and make it inconvenient for the user to operate. Therefore, dual-threaded work is often introduced, with the main thread responsible for updating the interface and scheduling, while the secondary thread is responsible for doing some blocking work.
After doing so, it will lead to a common problem, that is, many developers will update the contents of the interface in the secondary thread. For example, the following example:
In the above example, create the Win forms application and then add the following code:
Using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApp1 {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void button1_Click (object sender, EventArgs e) {var thread2 = new System.Threading.Thread (WriteTextUnsafe) Thread2.Start ();} private void WriteTextUnsafe () = > textBox1.Text = "This text was set unsafely.";}
Here, the use of threads to directly update the contents of the interface will lead to the following errors:
In this way, an exception pops up in the debug interface, but instead of solving the problem, some developers turn off the options for the development tool to keep the interface from popping up. Or do not use debugging.
In fact, the above code is problematic, and we need to modify it to the following form:
Using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApp1 {public partial class Form1: Form {public Form1 () {InitializeComponent () } private void button1_Click (object sender, EventArgs e) {var threadParameters = new System.Threading.ThreadStart (delegate {WriteTextSafe ("This text was set safely.");}); var thread2 = new System.Threading.Thread (threadParameters); thread2.Start () } public void WriteTextSafe (string text) {if (textBox1.InvokeRequired) {/ / Call this same method but append THREAD2 to the text Action safeWrite = delegate {WriteTextSafe ($"{text} (THREAD2)");}; textBox1.Invoke (safeWrite);} else textBox1.Text = text }}}
In this way, the problem must be solved. Delegation is used here.
This is the end of this article on "how to solve the error problem of multi-threaded interface update in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.