BackgroundWorker in Visual Basic .NET
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013
Visual Basic .net BackgroundWorker performs a task in the background, without affecting the form that executed the command. Sometimes it is useful to use this control when downloading a file, or copying a paste a file if the file is too large and takes a lot of time. After the task is done, BackgroundWorker send the result back to the form. The following is an example on how to use vb.net backgroundworker.
After you add BackgroundWorker control to a form, it appears in the bottom grey area of the designer:
Lets make a program that counts from 0 to 200 and show the result in a textbox. In this example we will add a button, a textbox, and a backgroundworker:
The button will be used to send the command to BackgroundWorker to start counting in the background.
Before we do this, lets adjust the BackgroundWorker1 properties to the following:
Double click on BackgroundWorker1 and add adjust the code the following
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) _ Handles BackgroundWorker1.DoWork For I = 0 To 200 'Telling the program to count from 0 - 200 TextBox1.Text = I 'Telling the program to show the integer I in TextBox1 BackgroundWorker1.ReportProgress(I) report the progress done by the ReportProgress System.Threading.Thread.Sleep(1000) stop after advancing one integer for 1 second. Next End Sub
To start BackgroundWorker1, double click on Button1 and add the following code to the click event:
If you start the program you will end up with the following error:
Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.
To bypass this error, add the following code to the load event:
Control.CheckForIllegalCrossThreadCalls = False
Start the program and click on Button1. Notice that the program will count to 200 in the background and show the result in the textbox.
Now lets see if we want to report the progress using a ProgressBar.
Add a ProgressBar to the form.
Adjust the BackgroundWorker1 progress changed event to the following:
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _ ByVal e As System.ComponentModel.ProgressChangedEventArgs) _ Handles BackgroundWorker1.ProgressChanged ProgressBar1.Value = e.ProgressPercentage End Sub
Run the program.
Lets also add a message box to notify the user when BackgroundWorker1 is done counting:
Add the following code: BackgroundWorker1 RunWorkerCompleted event:
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _ ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _ Handles BackgroundWorker1.RunWorkerCompleted MsgBox("Done") End Sub
We can also cancel the operation any time. To do that we will return to BackgroundWorker1 and the following highlighted code:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) _ Handles BackgroundWorker1.DoWork For I = 0 To 200 'Telling the program to count from 0 - 200 If BackgroundWorker1.CancellationPending = True Then e.Cancel = True Exit For Else TextBox1.Text = I 'Telling the program to show the integer I in TextBox1 'Telling the program to report the progress done by the ReportProgress BackgroundWorker1.ReportProgress(I) 'Telling the program to stop after advancing one integer for 1 second. System.Threading.Thread.Sleep(1000) End If Next End Sub
Then add a Cancel button and add the following code to the click event: