In the development of WinForm or WPF-related GUI programs, when encountering the problem of executing time-consuming program fragments and needing to present state information in real-time on the UI interface, in order to prevent the appearance of suspended animation, multithreading technology is used to access UI elements asynchronously across threads. The following summarizes some of the techniques commonly used in WinForm and WPF to access the UI interface asynchronously across threads.
Common technique in WinForm 1, adopt BackgroundWorker control
Public Partial classForm1:form {PrivateBackgroundWorker worker; PublicForm1 () {InitializeComponent (); Worker=NewBackgroundWorker (); Worker. Workerreportsprogress=true;//To report progress from the background worker, we need to setWorker. DoWork + = Worker_dowork;//This event is raised on the worker thread when the worker startsWorker. ProgressChanged + = worker_progresschanged;//This event would be raised if we call ReportProgress } Private voidWorker_progresschanged (Objectsender, ProgressChangedEventArgs e) {progressBar1.Value=E.progresspercentage; Lblpercent.text=string. Format ("{0}%", E.progresspercentage); if(E.progresspercentage = = -) {MessageBox.Show ("Data processing finished! "); } } Private voidWorker_dowork (Objectsender, DoWorkEventArgs e) { for(inti =1; I <= -; i++) { //Report progress to ' UI ' threadworker. ReportProgress (i); Thread.Sleep ( -); } } Private voidBtncompute_click (Objectsender, EventArgs e) { //Start the background workerworker. RunWorkerAsync (); } }2. Using the Timer control
Public Partial classForm1:form {PrivateTimer timer; Private intCurrent ; PublicForm1 () {InitializeComponent (); Timer=NewTimer (); Timer. Interval= -; Timer. Tick+=Timer_tick; } Private voidTimer_tick (Objectsender, EventArgs e) {progressBar1.Value=Current ; Lblpercent.text=string. Format ("{0}%", current); if(Current = = -) {timer. Stop (); MessageBox.Show ("Data processing finished! "); } } Private voidBtncompute_click (Objectsender, EventArgs e) {Thread Thread=NewThread (NewThreadStart (doWork)); Thread. Start (); Timer. Start (); } Private voiddoWork () { for(inti =1; I <= -; i++) { current=i; Thread.Sleep ( -); } } }3, the use of delegated way
Public Partial classForm1:form {Private Delegate voidUpdateProgress (intpercent); PublicForm1 () {InitializeComponent (); } Private voidBtncompute_click (Objectsender, EventArgs e) {Thread Thread=NewThread (NewThreadStart (doWork)); Thread. Start (); } Private voiddoWork () { for(inti =1; I <= -; i++) { //Progressbar1.begininvoke ((Action) (() = {progressBar1.Value = i;})); //Lblpercent.begininvoke ((Action) () = {Lblpercent.text = string. Format ("{0}%", I); })); //if (i = =)//{ //MessageBox.Show ("Data processing finished! "); //}update (i); Thread.Sleep ( -); } } Private voidUpdateintpercent) { if(invokerequired) { This. Invoke (Newupdateprogress (update), percent); } Else{progressBar1.Value=percent; Lblpercent.text=string. Format ("{0}%", percent); if(Percent = = -) {MessageBox.Show ("Data processing finished! "); } } } }Common Technologies in WPF
Dispatcher is introduced in WPF and is commonly used to troubleshoot updates to UI control state in non-UI threads.
/// <summary> ///the interactive logic of MainWindow.xaml/// </summary> Public Partial classMainwindow:window {Private Delegate voidUpdateProgress (intpercent); PublicMainWindow () {InitializeComponent (); } Private voidBtncompute_onclick (Objectsender, RoutedEventArgs e) {Task Task=NewTask (DoWork); Task. Start (); } Private voidDoWork () { for(inti =1; I <= -; i++) { //Dispatcher.begininvoke (Action) (() =//{ //progressBar1.Value = i; //lblpercent.content = string. Format ("{0}%", I); //if (i = =)// { //MessageBox.Show ("Data processing finished! "); // } //}));Dispatcher.begininvoke (Newupdateprogress (Update), i); Thread.Sleep ( -); } } Private voidUpdate (intpercent) {progressBar1.Value=percent; Lblpercent.content=string. Format ("{0}%", percent); if(Percent = = -) {MessageBox.Show ("Data processing finished! "); } } }
Common ways to update the UI with multithreading