相关文章推荐
爱听歌的手术刀  ·  【Android】[转] ...·  3 月前    · 
不羁的领带  ·  Structural ...·  10 月前    · 
逆袭的山寨机  ·  wpf 鼠标悬停事件-掘金·  1 年前    · 

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)
End Sub
Private Sub SetLabelText(ByVal text As String)
    If Label1.InvokeRequired Then
        Label1.Invoke(New Action(Of String)(AddressOf SetLabelText), text)
        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?

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) End Sub Private Sub pingem(p As Object) Dim pobj As pingObj = DirectCast(p, pingObj) Dim MyPing As New System.Net.NetworkInformation.Ping 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) End Sub Improve this answer Follow that makes sense, but could you explain the last sub, I don't quite understand how it works. – Postman Jul 26 '13 at 19:28 It is the thread that does the actual pings. Put some of your ip's in and add a breakpoint on the sleep. Look at the variables and it should make more sense. – dbasnett Jul 26 '13 at 19:54 Sorry for the delay in response, but you were right. I dissected it and merged it into my project, I understand it now, and it works great. thank you for that, will select as best answer when I can. – Postman Jul 26 '13 at 22:31 I am having a curious issue though. I have modified the code above quite a bit, but it's similar enough. Say I removed something from the thread, it will give me an error, I'm not sure exactly how to "stop" the thread(I know you shouldn't do that though). What would I do if I were to remove something from the listview? – Postman Jul 26 '13 at 23:32