# Solved: Using BackgroundWorker in VB.NET



## deadscenekid (Apr 26, 2006)

I was wondering if someone could give me an example of how to use the BackgroundWorker component in VB.NET/Visual Studio. I am not sure how it works, but I want to make it when the user clicks a button and loads a list of values from a server, my program does not hang while it is loading.

I want to make the BackgroundWorker do this:

```
Public Function RefreshStatusList()
        Dim users As LC.Users = Nothing
        Dim user1 As New LC.LosingColor(My.Settings.Username.ToString, My.Settings.Password.ToString)
        If My.Settings.TimeLine = 1 Then
            users = user1.GetTimeline(LC.Timeline.Friends)
        ElseIf My.Settings.TimeLine = 2 Then
            users = user1.GetTimeline(LC.Timeline.Public)
        ElseIf My.Settings.TimeLine = 3 Then
            users = user1.GetTimeline(LC.Timeline.User)
        ElseIf My.Settings.TimeLine = 4 Then
            users = user1.GetTimeline(LC.Timeline.Replies)
        End If
        lstStatuses.Columns.Add("Username", 100, HorizontalAlignment.Left)
        lstStatuses.Columns.Add("Status", 200, HorizontalAlignment.Left)
        lstStatuses.Columns.Add("Date", 150, HorizontalAlignment.Left)
        Try
            For Each user As LC.User In users
                Dim item As New ListViewItem(user.ScreenName)

                item.SubItems.Add(user.Status.Text)
                item.SubItems.Add(user.Status.CreatedTime)
                item.Tag = user
                lstStatuses.Items.Add(item)
                Me.txtCurrent.Text = user.Status.Text
                Me.LoadWebImageToPictureBox(PictureBox1, user.ProfileImageUrl)
            Next
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message)
        End Try
        lblUserName.Text = user1.Username
End Function
```
What I am asking is, I was wondering whether someone can show my how to use it and call it from a "Button_Click" event. I am quite experienced in VB, but I have never used a BackgroundWorker before.

Thanks.


----------



## deadscenekid (Apr 26, 2006)

Any help anyone? I would really like to know how to use the BackgroundWorker.


----------



## solanki.esha (Feb 16, 2009)

Hi

I just found a link on google. I hope it will help you. The code is self explanatory so I dont think you will have much problem.

http://www.java2s.com/Code/VB/GUI/BackgroundWorkerDemo.htm

You can place whatever code you need to execute in _DoWork method. Do tell me if it works.


----------



## deadscenekid (Apr 26, 2006)

Thanks for your reply. :]

Your link, along with the following link, helped a lot. Thank you for your help, it worked.

http://vbnotebookfor.net/2007/09/24/how-to-update-controls-using-backgroundworker-in-vbnet/


----------

