BackgroundWorker
This handles long-running tasks. It does not freeze the entire program as this task executes. The BackgroundWorker
type provides an excellent solution.
BackgroundWorker
enables a simple multithreaded architecture for VB.NET programs. This is more reliable, and easier, than trying to handle threads directly.
In the Toolbox pane, please double-click on the BackgroundWorker
icon. This will add a BackgroundWorker1
instance to the tray at the bottom of the screen.
BackgroundWorker
by right clicking on it and selecting Properties.BackgroundWorker1_DoWork
add some code. A Sleep method call can simulate some processor-intensive work.Public Class Form1
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles BackgroundWorker1.DoWork
' Do some time-consuming work on this thread.
System.Threading.Thread.Sleep(1000)
End Sub
End Class
RunWorkerAsync
How can you specify the BackgroundWorker
begins to do its work? You can use the RunWorkerAsync
method on the BackgroundWorker
instance.
BackgroundWorker
when the enclosing Form1
loads.Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Start up the BackgroundWorker1. BackgroundWorker1.RunWorkerAsync() End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) _ Handles BackgroundWorker1.DoWork ' Do some time-consuming work on this thread. System.Threading.Thread.Sleep(1000) End Sub End Class
RunWorkerCompleted
Now, we are going to tie the example together by adding another type to the program. We are adding the ArgumentType
, which has 2 integer fields.
Form1_Load
, we create the ArgumentType
, and then pass it to the RunWorkerAsync
method. This begins the DoWork
event handler.RunWorkerCompleted
is invoked after about one second, and we show the value on the screen (30).Public Class Form1 Public Class ArgumentType Public _a As Int32 Public _b As Int32 End Class Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Create the argument object. Dim args As ArgumentType = New ArgumentType() args._a = 5 args._b = 6 ' Start up the BackgroundWorker1. ' ... Pass argument object to it. BackgroundWorker1.RunWorkerAsync(args) End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) _ Handles BackgroundWorker1.DoWork ' Do some time-consuming work on this thread. System.Threading.Thread.Sleep(1000) ' Get argument. Dim args As ArgumentType = e.Argument ' Return value based on the argument. e.Result = args._a * args._b End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, _ ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _ Handles BackgroundWorker1.RunWorkerCompleted ' Called when the BackgroundWorker is completed. MessageBox.Show(e.Result.ToString()) End Sub End Class
BackgroundWorker
is useful for creating a background process, one which won't block the user interface. Disk or network accesses should be put on a background thread if possible.