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 classDatawrite { Public Delegate voidUpdateUI (intSTEP);//declares a delegate that updates the main thread PublicUpdateUI updateuidelegate; Public Delegate voidAccomplishtask ();//declares a delegate that notifies the main thread when a task is completed PublicAccomplishtask Taskcallback; Public voidWrite (Objectlinecount) {StreamWriter Writeio=NewStreamWriter ("Text.txt",false, Encoding.GetEncoding ("gb2312")); stringHead ="number, province, city"; Writeio.write (head); for(inti =0; I < (int) LineCount; i++) {writeio.writeline (i.tostring ()+", Hunan, Hengyang"); //writes a data call to the delegate that updates the main thread UI stateUpdateuidelegate (1); } //notifies the main thread when the task is complete to make corresponding processingTaskcallback (); Writeio.close (); } }
3. The code in the main interface is as follows: First, create a delegate to implement a thread update control that is not 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 voidBtnwrite_click (Objectsender, EventArgs e) { intTaskcount =10000;//task volume is 10000 This. Pgbwrite.maximum =Taskcount; This. Pgbwrite.value =0; Datawrite Datawrite=NewDatawrite ();//instantiate a class that writes dataDatawrite.updateuidelegate + = Updatauistatus;//binding a delegate that updates the status of a taskDatawrite.taskcallback + = accomplish;//binding the delegate to be invoked to complete the taskThread Thread=NewThread (NewParameterizedthreadstart (datawrite.write)); Thread. IsBackground=true; Thread. Start (Taskcount); } //Update UI Private voidUpdatauistatus (intStep) { if(invokerequired) { This. Invoke (NewAsynupdateui (Delegate(ints) { 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 (); } } //need to call when completing a task Private voidaccomplish () {//You can also do some other logical processing after the completion of the taskMessageBox.Show ("Task Complete"); }
The effect is as follows:
Summary 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. Source code: http://download.csdn.net/detail/mingge38/9378852
WinForm implementing multithreaded Asynchronous update UI (progress and body information)