This article mainly describes the C # WinForm implementation of multi-threaded asynchronous update UI (progress and status information), small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.
Introduction
In the WinForm program development needs to carry out a large number of data read and write operations, often will need a certain amount of time, but in this time period, the interface UI is not updated, resulting in the user appears to be in the state of suspended animation, resulting in a bad user experience. Therefore, in the application of a large number of data operations, multithreading is required to handle this situation. Using multithreading in C # is handy only if you need to use the Start method of an instance of System.Threading.Thread, but how to implement the interaction between multiple threads is not that simple. This article implements the use of sub-threads to process data, and real-time update the main thread of the UI state. The next step is to implement the asynchronous thread update UI demo program.
Application background
Write a certain amount of data into a text file, and need to reflect the real-time progress of writing data in the main interface. Requirement: Write data needs to be encapsulated into a class.
Implementation process
1, first set up a WinForm project, drag a button on the main form, a ProgressBar, a lable. As shown in.
2, write a processing data class (Writedate), the source code is as follows.
public class Datawrite {public delegate void UpdateUI (int step);//declares a delegate public that updates the main thread UpdateUI Updateuidelegate; public delegate void Accomplishtask ();//declares a delegate public Accomplishtask Taskcallback that notifies the main thread when the task is completed; public void Write (object linecount) { StreamWriter Writeio = new StreamWriter ("Text.txt", false, Encoding.GetEncoding ("gb2312")); String head = "number, province, city"; Writeio.write (head); for (int i = 0; i < (int) linecount; i++) { writeio.writeline (i.tostring () + ", Hunan, Hengyang"); Writes a data call to update the main thread UI state of the delegate Updateuidelegate (1); } When the task is complete, notify the main thread to make corresponding processing taskcallback (); Writeio.close (); } }
3. The code in the main interface is as follows:
The first step is to establish a delegate to implement a thread update control that does not create a control.
delegate void Asynupdateui (int step);
It then writes multithreading to start the method of writing the data and the function of the callback.
private void Btnwrite_click (object sender, EventArgs e) {int taskcount = 10000;//Task volume is 10000 this.pgbWrite.Maximum = Taskcount; This.pgbWrite.Value = 0; Datawrite datawrite = new Datawrite ();//Instantiate a class that writes data datawrite.updateuidelegate + = updatauistatus;//bind update task status Delegate Datawri Te. Taskcallback + = accomplish;//The delegate that the task is bound to invoke thread thread = new Thread (new Parameterizedthreadstart (Datawrite.write)); Thread. IsBackground = true; Thread. Start (Taskcount); }//Update UI private void Updatauistatus (int step) {if (invokerequired) {this. Invoke (New Asynupdateui (delegate (int s) {this.pgbWrite.Value + = s); This.lblWriteStatus.Text = this.pgbWrite.Value.ToString () + "/" + this.pgbWrite.Maximum.ToString (); }), step); } else {this.pgbWrite.Value + = step; This.lblWriteStatus.Text = this.pgbWrite.Value.ToString () + "/" + this.pgbWrite.Maximum.ToString (); }}//Completion of the task requires calling private void accomplish () {///can also perform some other logical processing after completion of the task MessageBox.Show ("task completion");}
The effect is as follows:
Summarize
There are a number of ways to implement an asynchronous update UI, but I think this is a more flexible way to get the status of a task in real time and handle it accordingly. This mode is also useful for using multiple threads to write different data to different files at the same time.