Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse a BackgroundWorker to perform a task in the background in Visual Basic .NET
DescriptionThis example shows how to use a BackgroundWorker to perform a task in the background in Visual Basic .NET.
Keywordsmulti-threading, threads, threading, multithreading, BackgroundWorker, background worker, Visual Basic .NET, VB.NET
CategoriesSoftware Engineering, VB.NET
 
The examples in the Multithreading section let you perform more than one task at the same time. Unfortunately using threads directly can be confusing. The BackgroundWorker component provides a slightly easier way to perform a background task.

The key pieces to the BackgroundWorker are:

  • RunWorkerAsync - The UI thread (the main program) should call this method to start the worker.
  • DoWork - The DoWork event is where the worker should do its work. It should periodically check its CancellationPending property to see if the UI thread is trying to stop it. If the worker is stopped, it should set the event's e.Cancel property to true.
  • CancelAsync - The UI thread can call this method to tell worker to stop. This sets the worker's CancellationPending to true.
  • ReportProgress - The worker can call this method to pass progress information back to the UI thread.
  • ProgressChanged - This event occurs when the worker calls ReportProgress. The UI thread can update labels, progress bars, and so forth to report progess to the user.
  • RunWorkerCompleted - This event occurs when the worker's DoEvent event handler ends. The UI thread can take action to clean up here. The e.Cancelled property tells whether the worker finished or was stopped.

The code for this example is relatively straightforward. Refer to the previous list as you look through the following code to see what each piece is supposed to do.

 
' Use the BackgroundWorker to perform a long task.
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    If (btnGo.Text = "Go") Then
        ' Start the process.
        lblStatus.Text = "Working..."
        btnGo.Text = "Stop"
        prgPercentComplete.Value = 0
        prgPercentComplete.Visible = True

        ' Start the BackgroundWorker.
        bgwLongTask.RunWorkerAsync()
    Else
        ' Stop the process.
        bgwLongTask.CancelAsync()
    End If
End Sub

' Perform the long task.
Private Sub bgwLongTask_DoWork(ByVal sender As _
    System.Object, ByVal e As _
    System.ComponentModel.DoWorkEventArgs) Handles _
    bgwLongTask.DoWork
    ' Spend 10 seconds doing nothing.
    For i As Integer = 1 To 10
        ' If we should stop, do so.
        If (bgwLongTask.CancellationPending) Then
            ' Indicate that the task was canceled.
            e.Cancel = True
            Exit For
        End If

        ' Sleep.
        System.Threading.Thread.Sleep(1000)

        ' Notify the UI thread of our progress.
        bgwLongTask.ReportProgress(i * 10)
    Next i
End Sub

' Update the progress bar.
Private Sub bgwLongTask_ProgressChanged(ByVal sender As _
    System.Object, ByVal e As _
    System.ComponentModel.ProgressChangedEventArgs) Handles _
    bgwLongTask.ProgressChanged
    prgPercentComplete.Value = e.ProgressPercentage
End Sub

' The long task is done.
Private Sub bgwLongTask_RunWorkerCompleted(ByVal sender As _
    System.Object, ByVal e As _
    System.ComponentModel.RunWorkerCompletedEventArgs) _
    Handles bgwLongTask.RunWorkerCompleted
    If (e.Cancelled) Then
        lblStatus.Text = "Canceled"
    Else
        lblStatus.Text = "Finished"
    End If
    btnGo.Text = "Go"
    prgPercentComplete.Visible = False
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated