问题
I have created a program which acts as both a simple local area network map, but also as a troubleshooting program for pinpointing faults (ie. determining where a connection might be broken). There is a main screen that looks like this:
Image 1
and each button links to subscreens, such as this:
Image 2
The program works well enough as is, but using the normal ping method it requires each ping to finish before sending the next one and can sometimes freeze up the program for several seconds while it waits for a response (especially in situations where multiple pings fail and a large amount of devices are being pinged, as in the second image).
Currently the way I am doing this is by fairly simple programming in which I ping each device based on a list in a .ini file and then update it's associated button backcolor and labels (one label for the Response time, and one to keep track of how many times the ping request fails). Here is a sample of my code:
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
What I want to do is have the screen load and function (rather than locking up until all pings finish) and update the buttons and labels as the pings replies come in. I'm not sure how to transfer the information about what button and labels to update with each ping when done asynchronously. Any advice would be great, also if there are suggestions on how to clean up this code by perhaps some kind of loop statement I'm open to that as well, I know my code is very simplistic and perhaps not done in the most efficient manner.
Thanks for any help you can provide.
回答1:
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");
Or watch this solution:
http://www.codeproject.com/Articles/481080/Continuous-asynchronous-Ping-using-TAP-and-IProgre
来源:https://stackoverflow.com/questions/30400219/modifying-vb-net-program-that-uses-normal-ping-method-to-use-async-ping-method