Home
VB.NET.WinForms
BackgroundWorker
This page was last reviewed on Sep 26, 2022.
Dot Net Perls
BackgroundWorker. This handles long-running tasks. It does not freeze the entire program as this task executes. The BackgroundWorker type provides an excellent solution.
Type notes. 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.
Tip You can change some properties of the BackgroundWorker by right clicking on it and selecting Properties.
Detail In this handler, you can execute any VB.NET code you want on the background thread.
And In 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.
So Let us look at the complete program so far. The program starts the 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.
Start In Form1_Load, we create the ArgumentType, and then pass it to the RunWorkerAsync method. This begins the DoWork event handler.
Info 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).
MessageBox.Show
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
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 26, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.