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?
Some very rough ideas.