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