From what has been done in the project for nearly one months, the time-consuming operation basically has to use the multi-threaded waiting form, the progress prompt form and so on to display the dynamic progress information in realtime. If the information is updated in real time directly on the main thread's form, it will cause the process of animation to be too fast or too slow. In order to alleviate these situations, this article makes reference to some articles, summarizes their wisdom in this. We hope to help you.
Create a wait form in multiple threads
In WinForm program development, computers often perform time-consuming tasks such as querying large amounts of data, complex business processes, and so on, which can take from a few seconds to a few 10 seconds, during which the WinForm program form no longer responds to any mouse and keyboard events. The user experience is poor in the presence of suspended animation.
A better solution is to display a wait form at the front of the interface during these tasks, telling the user that the task is in progress.
1.1 Development Wait Form
The form has a PictureBox control and two lable controls, and the Image property of the PictureBox control is a dynamic picture.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;usingNavmanager.common;namespacenavmanager.utils{ Public Partial classWaitform:form { PublicWaitform () {InitializeComponent (); SetText (""); } Private Delegate voidSettexthandler (stringtext); Public voidSetText (stringtext) { if( This. Label2. invokerequired) { This. Invoke (NewSettexthandler (SetText), text); } Else { This. Label2. Text =text; } } }}
wait for the form source
1.2 Provide access to the waiting form interface
Writing class Waitformservice
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Threading;usingSystem.Windows.Forms;namespacenavmanager.utils{/// <summary> ///Using Singleton Design Pattern/// </summary> Public classWaitformservice { Public Static voidCreatewaitform () {WaitFormService.Instance.CreateForm (); } Public Static voidClosewaitform () {WaitFormService.Instance.CloseForm (); } Public Static voidSetwaitformcaption (stringtext) {WaitFormService.Instance.SetFormCaption (text); } Private StaticWaitformservice _instance; Private Static ReadOnlyObject SyncLock =NewObject (); Public Staticwaitformservice Instance {Get { if(Waitformservice._instance = =NULL) { Lock(syncLock) {if(Waitformservice._instance = =NULL) {waitformservice._instance=NewWaitformservice (); } } } returnwaitformservice._instance; } } PrivateWaitformservice () {}PrivateThread Waitthread; PrivateWaitform Waitform; Public voidCreateForm () {if(Waitthread! =NULL) { Try{waitthread.abort (); } Catch(Exception) {}} Waitthread=NewThread (NewThreadStart (Delegate() {Waitform=NewWaitform (); Application.Run (Waitform); })); Waitthread.start (); } Public voidCloseform () {if(Waitthread! =NULL) { Try{waitthread.abort (); } Catch(Exception) {}}} Public voidSetformcaption (stringtext) { if(Waitform! =NULL) { Try{waitform.settext (text); } Catch(Exception) {}}} }}
waitformservice Class Code
1.3 Using the interface provided by the Waitformservice
Try { waitformservice.createwaitform (); = assembly.getexecutingassembly (); = ASMB. CreateInstance (className); as Form; This . ShowMenu (frm); Waitformservice.closewaitform (); } Catch (Exception ex) { waitformservice.closewaitform (); }
Use of Interfaces
Second, transparent data loading progress bar
Create a common progress bar control, which is used to improve the user experience each time the load is slow. The call is made by enabling the new thread.
The steps are as follows:
(1) Create a separate form that contains a progress bar control.
Set the style of the progress bar to Progressbarstyle.marquee;
Add an attribute (loadcompleted) to indicate whether the load is complete.
(2) The call is made in another form by opening a new thread.
Public Partial classUcloaddata_showmarqueeprocess:usercontrol { Public StaticFrmmarqueeprocess XF; Public Static EventEventHandler onloadfinished; Publicucloaddata_showmarqueeprocess () {InitializeComponent (); } Private voidBtnfill_click (Objectsender, EventArgs e) { intIend =10000; Thread T=NewThread (NewThreadStart (showprocess)); T.start (); for(inti =0; i < iend; i++) { stringStrcontent =string. Format ("This is the {0}th record", i); This. LSTCONTENT.ITEMS.ADD (strcontent); } onloadfinished+=NewEventHandler (loadfinish); if(Onloadfinished! =NULL) Onloadfinished.invoke (sender, E); while(!XF. loadcompleted) application.doevents (); T.abort (); } Private Static voidshowprocess () {XF=Newfrmmarqueeprocess (); Xf. ShowDialog (); } Private Static voidLoadfinish (Objectsender, EventArgs e) {XF. LoadCompleted=true; } }
Transparent progress bar source code
The results of the operation are as follows:
Reference articles
1. Falling paper Plane , C # WinForm in multi-threading Create Wait form, 2011.
2. Heoo, multi-thread progress bar ,.
C # Multi-threaded forms creation