ARTICLE

Updation of Controls using BackGroundWorker in VB.NET

Posted by Shalini Juneja Articles | ASP.NET using VB.NET April 04, 2011
In this article we demonstrate on what is BackGroundWorker,it's property and how you can update controls using BackGroundWorker.
Download Files:
 
Reader Level:


A Visual Basic backGroundWorker control is used to execute an operation on a seperate 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 ISite of the component. 
  • Container:- Gets the IContainer that contains the component. 
  • IsBusy:- Determine a value indicating whether the BackGroundWorker is running an Asyncronous 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 ListBox named lstValues controls. The form will display like below.

    Bgw.gif

    Bgw2.gif 
      
  • 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
        lstValues.Items.Clear()
        prgThread.Value = 0
        TestWorker = New System.ComponentModel.BackgroundWorker
        TestWorker.WorkerReportsProgress = True
        TestWorker.WorkerSupportsCancellation = True
        TestWorker.RunWorkerAsync()
    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)
            Threading.Thread.Sleep(100)
        Next
    End Sub
    Private Sub TestWorker_ProgressChanged(ByVal sender As Object, ByVal e As                    System.ComponentModel.ProgressChangedEventArgs) Handles
        TestWorker.ProgressChanged
        prgThread.Value = e.ProgressPercentage
        lstValues.Items.Add(e.UserState)
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        TestWorker.CancelAsync()
    End Sub
    Private Sub TestWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)         Handles
       
TestWorker.RunWorkerCompleted
        btnStart.Enabled = True
        btnCancel.Enabled = False
    End
Sub


OutPut:-

Bgw3.gif

bgw4.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor