vb.net pinging on multiple threads

别来无恙 提交于 2019-12-23 04:30:50

问题


I have around 13 computers in my house, and I want to be able to monitor them all by pinging them by their local IPs. I have a program set up to add ping targets to a dictionary and that dictionary is displayed in a listview.

Now, when I go to ping an IP, using:

    Dim MyPing As New System.Net.NetworkInformation.Ping
    Dim Myreply As System.Net.NetworkInformation.PingReply = MyPing.Send("192.168.1.10")
    MsgBox(Myreply.RoundtripTime)

It lags the program until it gets a response from the IP, or until it times out. I know that I need to ping each IP on a separate thread to avoid this, and use a delegate to update the results in my dictionary.

I've tried to understand many tutorials about using threads and delegates, but I just can't understand quite how they work, I need a really simple example that will allow me to pass 2 parameters into this new thread, and then updates my information on the UI thread (in my dictionary).

However, I patched this together from a tutorial or two a couple months ago, after trying to understand threads and delegates, eventually giving up. But it only allows you to pass one parameter, and I'm not sure if it will work when I'm making threads on the fly to ping on. Plus, I don't really understand how it works, and couldn't modify it for the life of me.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim t1 As New Threading.Thread(AddressOf Count)
    t1.IsBackground = True
    t1.Start(100)
End Sub


Private Sub Count(ByVal Max As Integer)
    For i = 1 To Max
        SetLabelText(i.ToString())
        Threading.Thread.Sleep(50)
    Next
End Sub


Private Sub SetLabelText(ByVal text As String)
    If Label1.InvokeRequired Then
        Label1.Invoke(New Action(Of String)(AddressOf SetLabelText), text)
    Else
        Label1.Text = text

    End If
End Sub

A couple of the things I've looked at:

http://www.developerfusion.com/article/5251/delegates-in-vbnet/

http://www.tek-tips.com/faqs.cfm?fid=6036

http://timhastings.co.uk/2011/09/vb-net-using-delegates-for-form-updates-from-worker-threads/

http://www.youtube.com/watch?v=h-DQywCJ_wY

So in summary, I need to ping an IP on a separate thread, and then change a value in my dictionary with the results of the ping (IE: timeout, roundtrip). And because I need to update my dictionary, I need to pass two parameters into the thread, an IP and a unique name the user makes for that computer. Could anyone point me in the right direction or provide a super simple example I can try to learn off of?


回答1:


Some very rough ideas.

Class pingObj
    Property addr As Net.IPAddress
    Property idx As Integer
End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ListBox1.Items.Clear()
    'ips to ping
    Dim ips As New List(Of String)({"192.168.1.1", "192.168.1.5", "192.168.1.6"})
    For Each i As String In ips
        ListBox1.Items.Add(i)
        Dim foo As New pingObj
        foo.addr = Net.IPAddress.Parse(i)
        foo.idx = ListBox1.Items.Count - 1
        Dim t As New Threading.Thread(AddressOf pingem)
        t.IsBackground = True
        t.Start(foo)
    Next
End Sub

Private Sub pingem(p As Object)
    Dim pobj As pingObj = DirectCast(p, pingObj)
    Dim MyPing As New System.Net.NetworkInformation.Ping
    Do
        Dim Myreply As System.Net.NetworkInformation.PingReply = MyPing.Send(pobj.addr)
        Me.Invoke(Sub() ListBox1.Items.Item(pobj.idx) = String.Format("{0} - {1}", _
                                                                     Myreply.Address.ToString, Myreply.RoundtripTime.ToString))
        Threading.Thread.Sleep(1000)
    Loop

End Sub


来源:https://stackoverflow.com/questions/17887422/vb-net-pinging-on-multiple-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!