The control event calls the DoWork () method on the line.
#regionProgress barPrivateBackgroundWorker worker =NULL; Private voidDoWork (stringoutfile) { //Show progress barProgressform Progressform =NewProgressform (); Progressform.topmost=true; Progressform.show ( This); //the count of the preparation progress barWorker =NewBackgroundWorker (); //specify to provide progress notificationsWorker. Workerreportsprogress =true; //provides interrupt functionalityWorker. Workersupportscancellation =true; //The main function of a thread is to handle events//turn on thread execution workWorker. DoWork + =NewDoworkeventhandler (worker_dowork); //specify the function to handle progress//Specify the features used to process progressWorker. ProgressChanged + =NewProgresschangedeventhandler (progressform.onprogresschanged); Worker. ProgressChanged+=NewProgresschangedeventhandler (worker_progresschanged); //The progress bar finishes the work//1. Work completed//2. Work Error Exception//3. Cancellation of workWorker. RunWorkerCompleted + =NewRunworkercompletedeventhandler (progressform.onprocesscompleted); Worker. RunWorkerCompleted+=NewRunworkercompletedeventhandler (worker_runworkercompleted); //If the progress bar requires parameters//Call System.ComponentModel.BackgroundWorker.RunWorkerAsync//Pass in your parameters to System.ComponentModel.BackgroundWorker.DoWork//Extracting Parameters//System.ComponentModel.DoWorkEventArgs.Argumentworker. RunWorkerAsync (outfile); } //single thread execution work Private voidWorker_dowork (Objectsender, DoWorkEventArgs e) { Try{worker_doing (BackgroundWorker) sender, E); } Catch(Exception ex) {
This is exception handling and cannot be placed inside the worker_doing E.result=ex; } } //to work Private voidworker_doing (BackgroundWorker worker, DoWorkEventArgs e) {stringoutfile = E.argument as string;//Incoming ParametersintCount =500; for(inti =0; I < count; i++) { //Check Cancel if(worker. cancellationpending) {E.cancel=true; Break; } Else{//Here is the specific work code
//...
Progress report worker. ReportProgress ((i+1) * ( -/count), "Progress Information"); //Thread HibernationThread.Sleep ( -); } } } //Progress Report Private voidWorker_progresschanged (Objectsender, ProgressChangedEventArgs e) {//Here The progress information can be displayed in the window
E.userstate As String
//...
} //Work Completion Status Private voidWorker_runworkercompleted (Objectsender, Runworkercompletedeventargs e) { stringMessage =""; if(E.error! =NULL) {Message=E.error.message;MessageBox.Show ( This, Message,"Exception"); } Else if(e.cancelled) {message="The operation was canceled. "; MessageBox.Show ( This, Message,"Tips"); } Else { if(E.result = =NULL) {Message="completed. "; MessageBox.Show ( This, Message,"Information"); } Else{message= (E.result asException). Message; MessageBox.Show ( This, Message,"Exception"); } }//The code here is done after the completion of the processing work
//... } #endregion
C # Asynchronous progress bar Component BackgroundWorker