Winform implements multi-thread asynchronous UI Update (progress and status information ),

Source: Internet
Author: User

Winform implements multi-thread asynchronous UI Update (progress and status information ),
Introduction

It usually takes some time to perform read and write operations on a large amount of data during Winform program development. However, during this time period, the ui cannot be updated, in the user's opinion, the interface is in a suspended state, resulting in poor user experience. Therefore, multithreading is required for a large number of data operations. To use multithreading in c #, you only need to use the Start method of an instance of System. Threading. Thread, but how to implement interaction between multithreading is not that simple. This article implements the use of sub-threads to process data and update the ui status of the main thread in real time. Next, let's start to implement the demo program of updating the ui by asynchronous threads step by step.

 

Application background

Write a certain amount of data to a text file, and reflect the real-time progress of Data Writing in the main interface. Requirement: write data needs to be encapsulated into a class.

Implementation Process

1. Create a winform project, drag a button, a progressbar, and a lable on the main form. As shown in.

2. Compile a data processing class (WriteDate). The source code is as follows.
Public class DataWrite {public delegate void UpdateUI (int step); // declare a delegate to update the main thread public UpdateUI UpdateUIDelegate; public delegate void AccomplishTask (); // declare a delegate that notifies the main thread when the task is completed public AccomplishTask TaskCallBack; public void Write (object lineCount) {StreamWriter writeIO = new StreamWriter ("text.txt", false, Encoding. getEncoding ("gb2312"); string head = "No., province, city"; writeIO. write (head); for (int I = 0; I <(int) lineCount; I ++) {writeIO. writeLine (I. toString () + ", Hunan, Hengyang"); // write a piece of data, call the delegate UpdateUIDelegate (1) that updates the ui status of the main thread );} // notify the main thread to handle TaskCallBack (); writeIO when the task is completed. close ();}}

 

3. The code in the main interface is as follows: first, you need to create a delegate to implement the thread update control for non-creation controls.
 delegate void AsynUpdateUI(int step);

 

Then write the multi-thread method to start writing data and the callback function.
Private void btnWrite_Click (object sender, EventArgs e) {int taskCount = 10000; // The number of tasks is 10000 this. pgbWrite. maximum = taskCount; this. pgbWrite. value = 0; DataWrite dataWrite = new DataWrite (); // instantiate a Data Writing Class dataWrite. updateUIDelegate + = UpdataUIStatus; // bind the delegate dataWrite that updates the task status. taskCallBack + = Accomplish; // bind the delegate Thread thread to be called to complete the task = 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 () ;}/// you need to call private void Accomplish () {// You can also perform other logic processing MessageBox after the task is completed. show ("task completed ");}
The effect is as follows: Summary There are many methods to Implement Asynchronous ui update, but I think this method is flexible and can get the status of the task in real time and process it accordingly. This mode also applies to using multiple threads to simultaneously write different data to different files. Source code: http://download.csdn.net/detail/mingge38/9378852

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.