1 Creating the BackgroundWorker control
Create a Windows Forms application, add the BackgroundWorker control under a blank Form1, and drag from the Toolbox.
We need to use several of the BackgroundWorker events:
DoWork event: Triggers an event when the RunWorkerAsync function is invoked to implement a daemon call.
ProgressChanged: Triggers an event when calling the ReportProgress function to implement progress tracking for the daemon. When you use ReportProgress to report progress updates, the Workerreportsprogress property should be true, or an exception will occur.
RunWorkerCompleted: Triggered when background program finishes, exceptions, or cancellations.
2 Create ProgressBar progress bar control
The value of the progressBar1.Value is from 0 to 100, and you should pay attention to the percentage when you use it.
3 Creating the Textbow control
Used to display background program update information.
Control code:
BackgroundWorker1//this.backgroundWorker1.WorkerReportsProgress = true;
This.backgroundWorker1.DoWork + = new System.ComponentModel.DoWorkEventHandler (this.backgroundworker1_dowork); This.backgroundWorker1.ProgressChanged + = new System.ComponentModel.ProgressChangedEventHandler (THIS.BACKGROUNDW
orker1_progresschanged); this.backgroundWorker1.RunWorkerCompleted + = new System.ComponentModel.RunWorkerCompletedEventHandler (
this.backgroundworker1_runworkercompleted);
PROGRESSBAR1//this.progressBar1.Location = new System.Drawing.Point (12, 12);
This.progressBar1.Name = "ProgressBar1";
This.progressBar1.Size = new System.Drawing.Size (428, 20);
This.progressBar1.TabIndex = 1;
This.progressBar1.Click + = new System.EventHandler (This.progressbar1_click);
TextBox1// This.textBox1.Location = new System.Drawing.Point (12, 38);
This.textBox1.Multiline = true;
This.textBox1.Name = "TextBox1";
This.textBox1.Size = new System.Drawing.Size (428, 53);
This.textBox1.TabIndex = 2; This.textBox1.TextChanged + = new System.EventHandler (this.textbox1_textchanged);
Event code:
private void Backgroundworker1_dowork (object sender, DoWorkEventArgs e) {for (int i = 0; i < 1000
; i++) {System.Threading.Thread.Sleep (10);
This.backgroundWorker1.ReportProgress (I/10, i);
} private void Backgroundworker1_progresschanged (object sender, ProgressChangedEventArgs e) {
This.progressBar1.Value = E.progresspercentage;
TextBox1.Text = E.userstate.tostring ();
} private void Backgroundworker1_runworkercompleted (object sender, Runworkercompletedeventargs e) {
This.progressBar1.Value = 100; private void Progressbar1_click (object sender, EventArgs e) {} private void TextBox1 _textchanged (object sender, EventArgs e) {} private void button2_click (object sender, EventArgs
e) {backgroundworker1.runworkerasync (); }
By clicking the button, calling Backgroundworker1.runworkerasync (), triggering the Backgroundworker1_dowork event, here with System.Threading.Thread.Sleep ( 10) The System sleep, reportprogress (I/10, i) reported 100 updates. Triggers the backgroundworker1_progresschanged event, gets progress and progress information, and displays it in the ProgressBar and TextBox, respectively.