Use of C # BackgroundWorker (background thread)

Source: Internet
Author: User

namespacebackgroundworkderpausesample{ Public Partial classMainform:form {BackgroundWorker worker=NULL;  PublicMainForm () {InitializeComponent (); Load+=NewEventHandler (Form1_Load); }        voidForm1_Load (Objectsender, EventArgs e) {Worker=NewBackgroundWorker (); Worker. Workerreportsprogress=true; Worker. Workersupportscancellation=true; Worker. DoWork+=NewDoworkeventhandler (worker_dowork); Worker. RunWorkerCompleted+=NewRunworkercompletedeventhandler (worker_runworkercompleted); Worker. ProgressChanged+=NewProgresschangedeventhandler (worker_progresschanged); }        voidWorker_progresschanged (Objectsender, ProgressChangedEventArgs e) {progressBar1.Value=E.progresspercentage; }        voidWorker_runworkercompleted (Objectsender, Runworkercompletedeventargs e) {            if(e.cancelled) MessageBox.Show ("the user canceled the Operation"); Else{MessageBox.Show ("The operation is completed normally"); }        }        voidWorker_dowork (Objectsender, DoWorkEventArgs e) {             for(inti =0; I < -; i++)            {                if(Worker. cancellationpending)//If the user requests a cancellation                {                     for(intK = i; K >=0; k--) {Thread.Sleep (Ten); Worker. ReportProgress (k);//simulate the effect of a rollback} e.cancel=true; return; } manualreset.waitone ();//if the ManualResetEvent is initialized to a terminating state (true), then the method will work until the reset signal is received.                Then, until the set signal is received, it continues to work. //vice versaThread.Sleep ( -); Worker. ReportProgress (i+1); }        }        Private voidBtstart_click (Objectsender, EventArgs e) {Worker.}        RunWorkerAsync (); }        Private voidBtcancel_click (Objectsender, EventArgs e) {Worker.}        CancelAsync (); }        PrivateManualResetEvent Manualreset =NewManualResetEvent (true); Private voidBtpause_click (Objectsender, EventArgs e) {            if(Btpause.text = ="Pause") {manualreset.reset ();//pauses the current thread's work, sends a signal to the WaitOne method, blocksBtpause.text ="continue to"; }            Else{manualreset.set ();//continue the work of a threadBtpause.text ="Pause"; }        }    }}

Use of C # BackgroundWorker (background thread)

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.