How to create and use multiple background workers from a for loop

Anybody have any ideas on how to create multiple background workers based on the amount of items a for loop picks up in a listview? See code snippet for example
for each i as listviewitem in listview.items
(What do i do hear?)
next

Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
  Ping(IP)
End Sub

Function Ping(ByRef IP As String) As Boolean
        Try
            Return My.Computer.Network.Ping(IP)
        Catch ex As Exception
            Return False
        End Try
        Return Nothing
    End Function

Open in new window

bryanpilgerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
Try

for each i as listviewitem in listview.items
     Dim BGW As New BackGroundWorker
     AddHandler BGW.DoWork, AddressOf(bgw_DoWork)
     BGW.RunWorkerAsync()
next
0
bryanpilgerAuthor Commented:
I add that in and it gives an error under the AddressOf(bgw_DoWork()) part. Error says: Argument not specified for parameter 'e' of 'Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)'.

Also, how am i going to pass the variable i to the background Worker? because i will replace IP in the Ping function.
0
CodeCruiserCommented:
Show me your code. You need this

AddHandler BGW.DoWork, AddressOf(bgw_DoWork)

to be

AddHandler BGW.DoWork, AddressOf bgw_DoWork

NOT
AddressOf(bgw_DoWork())
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bryanpilgerAuthor Commented:
I got it. you were correct. I removed the parenthesis, and then i also had to remove the 'handles me.dowork' on the background workers that it was creating. Thanks a lot for the assistance. below is the final code:

    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Dim counter As Integer = 0
        For Each i As ListViewItem In lvFriends.Items
            i.UseItemStyleForSubItems = False
            Dim item As New ListViewItem.ListViewSubItem
            item.Text = "Loading"
            item.Font = New System.Drawing.Font("Times New Roman", 9, FontStyle.Bold)
            item.ForeColor = Color.Blue
            i.SubItems.Add(item)
            counter += 1
        Next
        ReDim arrStatus(counter - 1)
        For i As Integer = 0 To UBound(arrStatus)
            Dim bgwFriendsUpdate As New BackgroundWorker
            AddHandler bgwFriendsUpdate.DoWork, AddressOf bgwFriendsUpdate_DoWork
            bgwFriendsUpdate.RunWorkerAsync(lvFriends.Items.Item(i))
        Next
    End Sub

    Private Sub bgwFriendsUpdate_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)

(Do my work)

    End Sub
0
CodeCruiserCommented:
Glad to help :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.