C # BackgroundWorker Implementation of the form does not die the progress bar function

Source: Internet
Author: User

The online example is a lot less configuration of the properties, changed to a direct use of:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 Ten namespaceXXXX One { A      Public Partial classBackgroundworker:form -     { -          PublicBackgroundWorker () the         { - InitializeComponent (); -Backgroundworker1.workerreportsprogress =true; -Backgroundworker1.workersupportscancellation =true; +Backgroundworker1.progresschanged + =NewProgresschangedeventhandler (backgroundworker1_progresschanged); -backgroundworker1.runworkercompleted + =NewRunworkercompletedeventhandler (backgroundworker1_runworkercompleted); +Backgroundworker1.dowork + =NewDoworkeventhandler (backgroundworker1_dowork); AProgressbar1.maximum =10000; at         } -  -         //This is where the display of the interface is handled by the response message . -         Private voidBackgroundworker1_progresschanged (Objectsender, ProgressChangedEventArgs e) -         { -              This. progressBar1.Value =E.progresspercentage; in              This. Label1. Text =e.userstate.tostring (); -              This. Label1. Update (); to         } +  -         //here is the background work after the completion of the message processing, you can do the following processing work.  the         Private voidBackgroundworker1_runworkercompleted (Objectsender, Runworkercompletedeventargs e) *         { $MessageBox.Show ("The operation was finally completed.");Panax Notoginseng         } -  the         //here is where the work function is called when the background process starts to work. You can write your existing processing functions here.  +         Private voidBackgroundworker1_dowork (Objectsender, DoWorkEventArgs e) A         { theWork This. BackgroundWorker1); +         } -  $         //the real deal work $         Private BOOLWork (BackgroundWorker BK) -         { -             intTatle =10001; the  -              for(inti =0; i < Tatle; i++)Wuyi             { the                 if(BK.) cancellationpending)//here to determine whether the user requested to cancel the background, and can exit early.  -                 { WuBk. ReportProgress (i, String.Format ("Current value is {0}, operation interrupted by user request", i)); -                     return false; About                 } $  -                 //Process , through this function, the main thread to report processing progress, preferably converted to a percentage, and the outside of the progress bar must correspond to the maximum value. Here, instead of translating, I have adjusted the maximum progress bar of the interface thread to match the total number here.  -Bk. ReportProgress (i, String.Format ("the current value is {0}", i)); -             } A             return true; +         } the  -         Private voidButton2_Click (Objectsender, EventArgs e) $         { the             //when the user requests cancellation, this is done. Sometimes it's not very good.  the              This. Backgroundworker1.cancelasync (); the         } the  -         Private voidButton1_Click (Objectsender, EventArgs e) in         { the             if( This. Backgroundworker1.isbusy) the                 return; About              This. Backgroundworker1.runworkerasync (); the         } the  the         Private voidButton3_Click (Objectsender, EventArgs e) +         { -              This. Close (); the         }Bayi     } the}


A few controls on the form can be

C # BackgroundWorker Implementation of the form does not die the progress bar function

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.