0

如果我多次运行我的操作,我会在我的 ListView 中为同一项目获得多个条目。

我正在创建一个简单的网络扫描器/主机名抓取器,它将在项目恢复到我的 ping 测试时将它们添加到列表视图中。

当我第一次运行它时,它运行良好并按应有的方式创建一个条目。

当我随后运行它时,它创建项目的次数与我运行代码前的次数一样多。第三次点击开始它会创建每个条目 3 次,而它应该只创建一次条目。

这是我的 go 按钮代码:

 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

这是我正在使用的一位后台工作人员的代码:

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
  • 我添加了 Hey 框,果然 ProgressChanged 事件触发了我点击 Go 按钮的次数。是我编码错误的东西吗?
4

1 回答 1

1

这很可能是因为您正在添加但没有删除您的处理程序以更改进度,因此您正在多次处理该事件。

在实例化后台工作人员时尝试添加进度更改事件处理程序,而不是每次单击按钮时。这样他们只会处理一次。

于 2013-02-07T00:17:53.037 回答