C # Asynchronous progress bar Component BackgroundWorker

Source: Internet
Author: User

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

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.