Go C # using BackgroundWorker

Source: Internet
Author: User

This article was reproduced from: http://blog.csdn.net/andrew_wx/article/details/6615077

This example uses BackgroundWorker to generate a number within the TextBox text that is less than 10000 and divisible by 5 (1 seconds to produce one)

The interface can start a thread, or it can stop threading, interface design

Put the code first, there is no explanation where the comment.

The entire Form1 form code is as follows

Introduce a namespace:

using system.threading;   using System.Net;  

Full code:

namespaceBackgroundworkerexample { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); Backgroundworker1.workerreportsprogress=true; Backgroundworker1.workersupportscancellation=true; Btn_stop.enabled=false; }            Private voidBtn_start_click (Objectsender, EventArgs e) {Txt_text.text="start generating random numbers within 10000 ... \ n"; Btn_start.enabled=false; Btn_stop.enabled=true; //start operation in background threadBackgroundworker1.runworkerasync (); }            Private voidBtn_stop_click (Objectsender, EventArgs e)              {Backgroundworker1.cancelasync (); Btn_stop.enabled=false; Btn_start.enabled=true; }            Private voidBackgroundworker1_dowork (Objectsender, DoWorkEventArgs e) {              //do not use the component instance name directly (BackgroundWorker1), because there are multiple BackgroundWorker,//Direct use creates a coupling problem that should be used by the following transformationsBackgroundWorker worker = Sender asBackgroundWorker; //The following content is equivalent to what the thread is dealing with. //Note: Do not interact with interface controls in this eventRandom r =NewRandom (); intNumcount =0;  while(Worker. Cancellationpending = =false)              {                  intnum = R.next (0,10000); if(num%5==0) {Numcount++; Worker. ReportProgress (0, num); Thread.Sleep ( +); }} E.result=Numcount; }            Private voidBackgroundworker1_progresschanged (Objectsender, ProgressChangedEventArgs e) {              intnum = (int) E.userstate; Txt_text.text+ = num +" "; }            Private voidBackgroundworker1_runworkercompleted (Objectsender, Runworkercompletedeventargs e) {              if(E.error = =NULL) Txt_text.text+="\ n \ nthe operation stopped, co-production"+ E.result +"a random number that can be divisible by 5."; ElseTxt_text.text+="\ nthe error occurred during operation:"+E.error; }      }  }  

This example randomly generates an integer on the worker thread of the BackgroundWorker component, and the worker thread runs the DoWork event handler, which is displayed on the form using the ProgressChanged event when an integer divisible by 5 is generated. When the program executes to the RunWorkerAsync method, the background thread is started. In the DoWork event, if the application does not cancel the background operation, it will not stop generating random integers and then determine whether the integer is divisible by 5, and if it can be divisible by 5, perform work. The ReportProgress method is used to trigger the ProgressChanged event, dealing with the interface long in the ProgressChanged event and displaying a random integer of production on the form.

Run after the interface is compiled

Go C # using BackgroundWorker

Related Article

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.