vb.net ping多线程 [英] vb.net pinging on multiple threads

查看:145
本文介绍了vb.net ping多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我家中大约有13台计算机,我希望能够通过其本地IP对它们进行ping操作来监视所有计算机.我有一个程序设置为向字典添加ping目标,并且该字典显示在列表视图中.

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.

现在,当我要ping IP时,请使用:

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)

它会滞后程序,直到它从IP处获得响应,或者直到它超时为止.我知道我需要在单独的线程上ping每个IP来避免这种情况,并使用委托来更新字典中的结果.

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.

我试图理解许多有关使用线程和委托的教程,但我只是不太了解它们如何工作,我需要一个非常简单的示例,该示例将允许我将2个参数传递给这个新线程,然后更新有关UI线程的信息(在我的词典中).

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).

但是,在尝试了解线程和委托并最终放弃之后,我在两个月前的一个教程中将其修补在一起.但是它只允许您传递一个参数,而且我不确定在动态创建线程以进行ping操作时它是否会起作用.另外,我不太了解它是如何工作的,也无法一生修改它.

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

我看过的几件事:

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

因此,总而言之,我需要在单独的线程上ping IP,然后使用ping的结果(IE:超时,往返)更改字典中的值. 而且由于需要更新字典,因此需要将两个参数传递给线程,分别是IP和用户为该计算机创建的唯一名称.谁能指出我正确的方向,还是提供一个我可以尝试学习的超级简单的例子?

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.

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

这篇关于vb.net ping多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆