Using BackGround Worker For Updation Of Controls In ASP.NET Using VB.NET

In this article we demonstrate on what is BackGroundWorker,it's property and how you can update controls using BackGroundWorker.
  • 3790

A Visual Basic backGroundWorker control is used to execute an operation on a separate thread. If you want to use BackGroundWorker control you have to import System. ComponentModel in to the project namespace.
The inheritance hierarchy for BackGroundWorker control is given below.

  • System.Object 
  • System.MarshalByRefObject 
  • System.ComponentModel.Component 
  • System.ComponentModel.BackGroundWorker
Properties of BackGroundWorker control
  • Site:-  It will get or set Site of the component. 
  • Container:- Gets the IContainer that contains the component. 
  • IsBusy:- Determine a value indicating whether the BackGroundWorker is running an Asynchronous operation. 
  • DesignMode:- Get a value that indicates whether the component is currently in design mode.
Here we are discussing how to update controls using BackGroundWorker
  • First you will need to create new windows application. 
  • On your form you will add a BackGroundWorker, two buttons named btnStart and btnCancel, a ProgressBar named prgThread and a List Box named lstValues controls. The form will display like below.


  • Now you have to add System. ComponentModel namespace on to the Project. 
  • Now add the below code.

   Private WithEvents TestWorker As System.ComponentModel.BackgroundWorker
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        btnStart.Enabled = False
        btnCancel.Enabled = True
        prgThread.Value = 0
        TestWorker = New System.ComponentModel.BackgroundWorker
        TestWorker.WorkerReportsProgress = True
        TestWorker.WorkerSupportsCancellation = True
    End Sub
    Private Sub TestWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles TestWorker.DoWork
        Dim ListText As String
        For Value As Integer = 0 To 100
            If TestWorker.CancellationPending Then
                Exit For
            End If
            ListText = String.Concat("Item #", Value)
            TestWorker.ReportProgress(Value, ListText)
    End Sub
    Private Sub TestWorker_ProgressChanged(ByVal sender As Object, ByVal e As                    System.ComponentModel.ProgressChangedEventArgs) Handles
        prgThread.Value = e.ProgressPercentage
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    End Sub
    Private Sub TestWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)         Handles
        btnStart.Enabled = True
        btnCancel.Enabled = False
    End Sub





More Articles

© 2020 DotNetHeaven. All rights reserved.