VB.NET BackgroundWorker

The VB.NET programming language

BackgroundWorker handles long-running tasks. It does not freeze the entire program as this task executes. The BackgroundWorker type provides an excellent solution. It enables a simple multithreaded architecture for VB.NET programs.

Start

Note

To begin, you will need a VB.NET Windows Forms project open. 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.

Tip:You can change some properties of the BackgroundWorker by right clicking on it and selecting Properties.

Also:You can click on the lightning bolt and add event handlers to your C# code file.

DoWork

Plus

Let's first add the DoWork event handler into the VB.NET code. In this handler, you can execute any VB.NET code you want on the background thread. In Properties, click on the lightning bolt and then double-click on the DoWork row.

And:At this point, a BackgroundWorker1_DoWork event handler is created. A Sleep method call can simulate some processor-intensive work.

BackgroundWorker
Class that uses DoWork event handler: VB.NET

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

Arrow indicates movement

So now you have the DoWork event handler in your VB.NET code. But how can you specify the BackgroundWorker begins to do its work? You can use the RunWorkerAsync method on the BackgroundWorker instance.

So:Let's look at the complete program so far. The program starts the BackgroundWorker when the enclosing Form1 loads.

Class that uses Load and RunWorkerAsync: VB.NET

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

Program icon

Now, we are going to tie the example together by adding another type to the program. We are adding the ArgumentType, which encapsulates two integer fields. These are the parameters to the BackgroundWorker.

In Form1_Load, we create the ArgumentType, and then pass it to the RunWorkerAsync method. This begins the DoWork event handler. In this handler, we call Sleep and then return the two fields multiplied together.

Finally:RunWorkerCompleted is invoked after about one second, and we show the value on the screen (30).

Class that introduces ArgumentType, RunWorkerCompleted: VB.NET

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

Summary

Framework: NET

BackgroundWorker is useful for creating a background process, one which won't block the user interface if it takes a long time to run. Often, disk or network accesses are the slowest and should be put on a background thread if possible.

Review:The BackgroundWorker is both effective and relatively easy to use once you understand its concept.


VB.NET: Windows