user2048863
user2048863

Reputation: 5

VB.net ListView Adding Items Multiple Times

I am having an issue where I get multiple entries in my ListView for the same item if I run my action more than once.

I am creating a simple network scanner/hostname grabber that will add the items to the listview as they come back alive to my ping test.

When I run it the first time it runs fine and creates one entry as it should.

When I run it subsequent times it creates the item as many times as I have ran the code ex. 3rd time hitting start it creates each entry 3 times when it should just create the entry once.

Here is my go button code:

 Private Sub Go_Click(sender As Object, e As EventArgs) Handles Go.Click
        Dim verifyIP
        ListView1.Items.Clear()
        chkDone = 0
        verifyIP = ipChk(ipAdd.Text)
        If verifyIP = 1 Then
            ipAddy = Split(ipAdd.Text, ".")
            pingTest1.WorkerReportsProgress = True
            pingTest1.WorkerSupportsCancellation = False
            AddHandler pingTest1.ProgressChanged, AddressOf pingTest1_ProgressChanged
            pingTest1.RunWorkerAsync()
            pingTest2.WorkerReportsProgress = True
            pingTest2.WorkerSupportsCancellation = False
            AddHandler pingTest2.ProgressChanged, AddressOf pingTest2_ProgressChanged
            pingTest2.RunWorkerAsync()
            pingTest3.WorkerReportsProgress = True
            pingTest3.WorkerSupportsCancellation = False
            AddHandler pingTest3.ProgressChanged, AddressOf pingTest3_ProgressChanged
            pingTest3.RunWorkerAsync()
            pingTest4.WorkerReportsProgress = True
            pingTest4.WorkerSupportsCancellation = False
            AddHandler pingTest4.ProgressChanged, AddressOf pingTest4_ProgressChanged
            pingTest4.RunWorkerAsync()
            While chkDone < 4
                wait(25)
            End While
        Else
            MsgBox("IP Invalid")
        End If
        MsgBox("Done")
    End Sub

Here is the code from one of the background workers I am using:

Private WithEvents pingTest1 As BackgroundWorker = New BackgroundWorker

    Private Sub pingTest1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles pingTest1.DoWork
        Try
            Dim hostCheck
            pingResult1 = 0
            pingTestDone1 = 0
            tryIP1 = ipAddy(0) & "." & ipAddy(1) & "." & ipAddy(2) & ".1"
            If My.Computer.Network.Ping(tryIP1) = True Then
                'Dim pingsender As New Net.NetworkInformation.Ping
                'If pingsender.Send(tryIP).Status = Net.NetworkInformation.IPStatus.Success Then
                Try
                    'Dim host As System.Net.IPHostEntry
                    hostCheck = ""
                    'host = System.Net.Dns.GetHostByAddress(tryIP3)
                    'MsgBox(host.HostName)
                    'host3 = host.HostName
                    'hostCheck = System.Net.Dns.GetHostEntry(tryIP3).HostName
                    hostCheck = System.Net.Dns.GetHostByAddress(tryIP1)
                    'get the hostname property 
                    hostCheck = hostCheck.HostName
                    pingTest1.ReportProgress("1", hostCheck)
                Catch f As Exception
                    'MsgBox("Error: " & f.Message)
                    pingTest1.ReportProgress("1", "No Hostname Found")
                End Try
            Else
                pingResult1 = 2
            End If
        Catch d As Exception
            MsgBox("There was an error trying to ping the IP Address: " & d.Message)
        End Try
    End Sub

    Private Sub pingTest1_ProgressChanged(e.ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
        MsgBox("Hey")
        Dim str(2) As String
        Dim itm As ListViewItem
        str(0) = tryIP1 & " Is Alive!!!"
        str(1) = e.UserState
        itm = New ListViewItem(str)
        ListView1.Items.Add(itm)
        str(0) = ""
        str(1) = ""
        itm = Nothing
    End Sub

Private Sub pingTest1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles pingTest1.RunWorkerCompleted
    chkDone = chkDone + 1
End Sub

Upvotes: 0

Views: 2014

Answers (1)

PGallagher
PGallagher

Reputation: 3113

It's most likely because you're adding, but not removing your handlers for the progress changed, so you're handling the event multiple times.

Try adding your Progress Changed Event Handlers when you're instantiating your Background workers, rather than every time you click your button. This way they will only handled once.

Upvotes: 1

Related Questions