BackgroundWorker Component

Lets you run time consuming processes in the background on a separate thread and leaving the User Interface responsive.
RunWorkerSync method raises the DoWork event
Put the code in the DoWork that you want to run in a separate thread
- Drag a backgroundWorker component to your form

- Double-click the backgroundWorker component in the component tray

- Add code to the DoWork event handler that you want to run in another process
For x = 1 To 500000000
'
Next
- This keeps the computer busy for a little bit

Start the background work
- Add a button control
- Name it btnStart
- Change the text to Start
- Add the following code to the button_click event handler
BackgroundWorker1.RunWorkerAsync()

Announce completion of a background process
When the background process is done the RunWorkerCompleted event is raised
- Select the backgroundWorker component in the component tray
- Select the lightning bolt in the properties window
- Double-click the RunWorkerCompleted event
- Add this code:
MessageBox.Show("Background process completed")

Cancelling a Background Process
You can call CancelAsync method, it sets the CancellationPending property of the backgroundWorker component to true
- Select the backgroundWorker component
- Set the WorkerSupportsCancellation property to true
- Add another button to your form
- Name it btnCancel
- Set the text property to Cancel
- Double-click the button and add this code to the button_click event handler
BackgroundWorker1.CancelAsync()
- If you forget to set the WorkerSupportsCancellation property you will get the following error

- Add the following code to the doWork method to cancel the operation. Put it inside your for loop.
If BackgroundWorker1.CancellationPending Then
e.Cancel = True
Exit Sub
End If
- When you cancel you get the messagebox stating that your process is complete.
- RunWorkerCompleted runs if either the process completes or is cancelled

Passing a parameter
- You can pass a parameter when you call RunWorkerSync
- It can be accessed by the DoWorkEventArgs.Argument property
Returning a value from a process
- Set the result property of the doworkeventargs in the dowork eventhandler
- Then retrieve it in the runworkercompleted event handler as result property in runworkercompletedeventargs
Example:
- Change the code in your btnStart_click event handler to
BackgroundWorker1.RunWorkerAsync("My Message")
- Change the code in your doWork to:
Dim myParameter As String
myParameter = CType(e.Argument, String)
e.Result = myParameter
- You have to cast it to the correct data type
- You could now use this in your process if you wanted

- Change the code in you runWorkerCompleted event handler to
MessageBox.Show("Background process completed" + e.Result.ToString())
- If you cancel the process you get an error, so you might want to wrap this code in a try statement or put it in an if statement
If e.Cancelled Then
MessageBox.Show("Background process cancelled ")
Else
MessageBox.Show("Background process completed " + e.Result.ToString())
End If

Requesting Status of Backgroundworker
You can tell if the background worker is executing a process from the isBusy Property
- Add another button
- Name it btnCheckBW
- Change the text to Check
- Add this code to the button click event handler
If BackgroundWorker1.IsBusy Then
MessageBox.Show("backgroundWorker is busy")
Else
MessageBox.Show("backgroundWorker is Ready")
End If

Reporting progress with backgroundWorker
- The backgroundWorker has a reportProgress method that raises the backgroundWorker.ProgressChanged event
- Lets you pass a parameter that tells the percent of progress
- You have to change the workerReportsProgress property to true
- Add a progressbar to the form
- Set the maximum property to 500000000
- Add this code inside your for loop in the dowork event handler
If x Mod 5000000 = 0 Then
BackgroundWorker1.ReportProgress(x)
End If
- The mod is added to reduce the amount of updates
- Test it
