修改使用常规ping方法的VB.Net程序以使用异步ping方法 [英] Modifying VB.Net program that uses normal ping method to use Async ping method
问题描述
我创建了一个程序,它既可以用作简单的局域网图,也可以用作查明故障的故障排除程序(例如,确定连接可能断开的地方).有一个主屏幕如下所示:
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:
每个按钮都链接到子屏幕,例如:
and each button links to subscreens, such as this:
程序照常运行良好,但是使用正常的ping方法,它要求每个ping在发送下一个ping之前完成,并且有时它会在等待响应的过程中冻结该程序几秒钟(尤其是在多次响应的情况下) ping失败,并且正在ping大量设备,如第二幅图所示.
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).
当前,我这样做的方法是通过相当简单的编程,在该程序中,我根据.ini文件中的列表对每个设备执行ping操作,然后更新其相关的按钮背景色和标签(一个标签表示响应时间",另一个标签表示响应时间").跟踪ping请求失败的次数).这是我的代码示例:
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
我想做的是拥有屏幕负载和功能(而不是锁定直到所有ping操作完成),并在ping答复进入时更新按钮和标签.我不确定如何传输有关哪些内容的信息异步完成时,按钮和标签会随着每次ping进行更新.任何建议都是很好的,如果有人建议通过某种我可能对此感兴趣的循环语句来清理此代码,我也知道我的代码非常简单,也许无法以最有效的方式完成
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.
推荐答案
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/使用TAP和IProgre的连续异步Ping
这篇关于修改使用常规ping方法的VB.Net程序以使用异步ping方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!