修改使用普通ping方法的VB.Net程序使用Async ping方法

时间:2015-05-22 15:10:36

标签: vb.net asynchronous ping

我创建了一个程序,既可以作为简单的局域网地图,也可以作为故障排除程序来查明故障(即确定连接可能被破坏的位置)。主屏幕看起来像这样:

Image 1

并且每个按钮链接到子屏幕,例如:

Image 2

该程序原样运行良好,但是使用普通的ping方法,它需要在发送下一个ping之前完成每个ping,并且有时在等待响应时冻结程序几秒钟(特别是在多个情况下) ping失败并且正在ping大量设备,如第二张图片中所示。

目前我这样做的方式是通过相当简单的编程,我根据.ini文件中的列表ping每个设备,然后更新它的相关按钮背景颜色和标签(响应时间的一个标签) ,和一个跟踪ping请求失败的次数)。以下是我的代码示例:

Dim INI_File As New IniFile(My.Application.Info.DirectoryPath + "/IPAddresses.ini")

Public Sub pingallmills()
    Dim MyPing As New System.Net.NetworkInformation.Ping


    Dim MyEMillPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "E-Mill", "(none)"), 200)
    Label4.Text = INI_File.GetString("Main Screen", "E-Mill", "(none)")
    If MyEMillPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button1.BackColor = Color.LightGreen
        Label2.Text = MyEMillPing.RoundtripTime & " ms"
    Else
        Button1.BackColor = Color.LightSalmon
        Label100.Text = Label100.Text + 1
    End If

    Dim MyAMillPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "A-Mill", "(none)"), 200)
    Label5.Text = INI_File.GetString("Main Screen", "A-Mill", "(none)")
    If MyAMillPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button2.BackColor = Color.LightGreen
        Label7.Text = MyAMillPing.RoundtripTime & " ms"
    Else
        Button2.BackColor = Color.LightSalmon
        Label98.Text = Label98.Text + 1
    End If

    Dim MyCMillPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "C-Mill", "(none)"), 400)
    Label9.Text = INI_File.GetString("Main Screen", "C-Mill", "(none)")
    If MyCMillPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button3.BackColor = Color.LightGreen
        Label11.Text = MyCMillPing.RoundtripTime & " ms"
    Else
        Button3.BackColor = Color.LightSalmon
        Label96.Text = Label96.Text + 1
    End If

    Dim MyAtoCPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "AtoC", "(none)"), 200)
    Label14.Text = INI_File.GetString("Main Screen", "AtoC", "(none)")
    If MyAtoCPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Panel1.BackColor = Color.LightBlue
        Label16.Text = MyAtoCPing.RoundtripTime & " ms"
    Else
        Panel1.BackColor = Color.LightSalmon
        Label102.Text = Label102.Text + 1
    End If

    Dim MyCtoAPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "CtoA", "(none)"), 200)
    Label20.Text = INI_File.GetString("Main Screen", "CtoA", "(none)")
    If MyCtoAPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Panel2.BackColor = Color.LightBlue
        Label22.Text = MyCtoAPing.RoundtripTime & " ms"
    Else
        Panel2.BackColor = Color.LightSalmon
        Label104.Text = Label104.Text + 1
    End If

    Dim MyAOfficesPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "AOffices", "(none)"), 400)
    Label18.Text = INI_File.GetString("Main Screen", "AOffices", "(none)")
    If MyAOfficesPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button4.BackColor = Color.LightGreen
        Label25.Text = MyAOfficesPing.RoundtripTime & " ms"
    Else
        Button4.BackColor = Color.LightSalmon
        Label94.Text = Label94.Text + 1
    End If

    Dim MyCtoFabPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "CtoFab", "(none)"), 200)
    Label28.Text = INI_File.GetString("Main Screen", "CtoFab", "(none)")
    If MyCtoFabPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Panel3.BackColor = Color.LightBlue
        Label30.Text = MyCtoFabPing.RoundtripTime & " ms"
    Else
        Panel3.BackColor = Color.LightSalmon
        Label106.Text = Label106.Text + 1
    End If

    Dim MyFabtoCPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "FabtoC", "(none)"), 200)
    Label33.Text = INI_File.GetString("Main Screen", "FabtoC", "(none)")
    If MyFabtoCPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Panel4.BackColor = Color.LightBlue
        Label35.Text = MyFabtoCPing.RoundtripTime & " ms"
    Else
        Panel4.BackColor = Color.LightSalmon
        Label49.Text = Label49.Text + 1
    End If

    Dim MyFabshopPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "FabShop", "(none)"), 400)
    Label37.Text = INI_File.GetString("Main Screen", "Fabshop", "(none)")
    If MyFabshopPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button7.BackColor = Color.LightGreen
        Label39.Text = MyFabshopPing.RoundtripTime & " ms"
    Else
        Button7.BackColor = Color.LightSalmon
        Label45.Text = Label45.Text + 1
    End If

    Dim MyElecShopPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "ElecShop", "(none)"), 400)
    Label51.Text = INI_File.GetString("Main Screen", "ElecShop", "(none)")
    If MyElecShopPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button6.BackColor = Color.LightGreen
        Label53.Text = MyElecShopPing.RoundtripTime & " ms"
    Else
        Button6.BackColor = Color.LightSalmon
        Label47.Text = Label47.Text + 1
    End If

    Dim MySMillPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "S-Mill", "(none)"), 200)
    Label59.Text = INI_File.GetString("Main Screen", "S-Mill", "(none)")
    If MySMillPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button8.BackColor = Color.LightGreen
        Label61.Text = MySMillPing.RoundtripTime & " ms"
    Else
        Button8.BackColor = Color.LightSalmon
        Label55.Text = Label55.Text + 1
    End If

    Dim MyContainerPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "Container", "(none)"), 200)
    Label71.Text = INI_File.GetString("Main Screen", "Container", "(none)")
    If MyContainerPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button10.BackColor = Color.LightGreen
        Label73.Text = MyContainerPing.RoundtripTime & " ms"
    Else
        Button10.BackColor = Color.LightSalmon
        Label43.Text = Label43.Text + 1
    End If

    Dim MyDMillOfficesPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "DOffices", "(none)"), 200)
    Label67.Text = INI_File.GetString("Main Screen", "DOffices", "(none)")
    If MyDMillOfficesPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button9.BackColor = Color.LightGreen
        Label69.Text = MyDMillOfficesPing.RoundtripTime & " ms"
    Else
        Button9.BackColor = Color.LightSalmon
        Label57.Text = Label57.Text + 1
    End If

    Dim MyDMillPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "D-Mill", "(none)"), 400)
    Label75.Text = INI_File.GetString("Main Screen", "D-Mill", "(none)")
    If MyDMillPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button11.BackColor = Color.LightGreen
        Label77.Text = MyDMillPing.RoundtripTime & " ms"
    Else
        Button11.BackColor = Color.LightSalmon
        Label92.Text = Label92.Text + 1
    End If

    Dim MyServerRoomPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "ServerRoom", "(none)"), 200)
    Label87.Text = INI_File.GetString("Main Screen", "ServerRoom", "(none)")
    If MyServerRoomPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button12.BackColor = Color.LightGreen
        Label89.Text = MyServerRoomPing.RoundtripTime & " ms"
    Else
        Button12.BackColor = Color.LightSalmon
        Label42.Text = Label42.Text + 1
    End If

    Dim MyMechanicShopPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "MechanicShop", "(none)"), 200)
    Label114.Text = INI_File.GetString("Main Screen", "MechanicShop", "(none)")
    If MyMechanicShopPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Button13.BackColor = Color.LightGreen
        Label116.Text = MyMechanicShopPing.RoundtripTime & " ms"
    Else
        Button13.BackColor = Color.LightSalmon
        Label111.Text = Label111.Text + 1
    End If

    Dim MyFuelStationPing As System.Net.NetworkInformation.PingReply = MyPing.Send(INI_File.GetString("Main Screen", "FuelStation", "(none)"), 200)
    Label227.Text = INI_File.GetString("Main Screen", "FuelStation", "(none)")
    If MyFuelStationPing.Status = Net.NetworkInformation.IPStatus.Success Then
        Panel32.BackColor = Color.LightGreen
        Label226.Text = MyFuelStationPing.RoundtripTime & " ms"
    Else
        Panel32.BackColor = Color.LightSalmon
        Label222.Text = Label222.Text + 1
    End If

End Sub

我想要做的是拥有屏幕加载和功能(而不是在所有ping完成之前锁定)并在ping回复进入时更新按钮和标签。我不知道如何传输信息关于异步完成时每次ping更新的按钮和标签。任何建议都会很棒,如果有关于如何通过某种循环语句来清理这些代码的建议,我也很开放,我知道我的代码非常简单,也许最多也没有完成高效的方式。

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

        IPAddress ip_addr1;
        IPAddress ip_addr2;
        IPAddress ip_addr3;

        // calling an asynchronous operations.
        var result1 = new System.Net.NetworkInformation.Ping().SendPingAsync(ip_addr1);
        var result2 = new System.Net.NetworkInformation.Ping().SendPingAsync(ip_addr2);
        var result3 = new System.Net.NetworkInformation.Ping().SendPingAsync(ip_addr3);

        // do another initialization stuff

        // check result
        if (result1.Result.Status == IPStatus.Success)
            Console.WriteLine("1: Sucess");
        else
            Console.WriteLine("1: Failure");

        if (result2.Result.Status == IPStatus.Success)
            Console.WriteLine("2: Sucess");
        else
            Console.WriteLine("2: Failure");      

或者观看此解决方案:

http://www.codeproject.com/Articles/481080/Continuous-asynchronous-Ping-using-TAP-and-IProgre