我正在开发一个可以ping通多个主机的应用程序。主机列表是从CSV文件中读取的。
当有响应时,程序显示绿色勾号,ping失败时显示红色叉号。
这工作得很好,但是当往返时间超过50ms
时,我需要显示第三个图像(如黄色解释标记)。
这是我现在的代码:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(500);
Parallel.For(0, ipAddress.Count(), (i, loopState) =>
{
Ping ping = new Ping();
PingReply pingReply = ping.Send(ipAddress[i].ToString());
this.BeginInvoke((Action)delegate ()
{
pictureboxList[i].BackgroundImage = (pingReply.Status == IPStatus.Success) ? Image.FromFile(@"C:UsersPCDownloadsgreen.png") : Image.FromFile(@"C:UsersPCDownloadsred.png");
});
});
}
有办法吗?
使用List<Task>
生成Ping请求序列的简单示例,使用调用者提供的IP地址集合(以字符串形式)并使用IProgressProgress<T>
捕获当前的SynchronizationContext,所以由委托执行的代码,在初始化它的线程中执行;UI线程,这里)。
对于传递给该方法的每个地址,一个PingAsync()
任务被添加到列表中。
PingAsync()
方法调用Ping.SendPingAsync()并报告结果,无论是成功还是失败,作为一个对象,可以以SocketError
的形式表示PingReply, PingException或SocketException (Progress()
方法将SocketError
转换为IPStatus,以处理一种类型的结果。如果您需要更详细的回复,请添加更多的案例)。
任务生成一个序列(一个int
值),它被发送给Progress<T>
代表,以防万一它需要它。这里,它用于从传递给PingAll()
方法的集合中选择特定的控件。
然后您可以在进程中处理这些结果。委托,以查看当前Ping请求发生了什么,并更新您的控件。
然后等待Task.WhenAll()。它将在所有任务完成时返回。当Ping成功或失败或超过指定的超时时间时,任务完成。显示结果状态的3张图片:
- Green - IPStatus。成功和往返时间<= 30
- 黄色- ip状态。成功与往返时间30
- Red - IPStatus != IPStatus。成功
取自项目资源。最好不要从这里的文件系统中获取它们,您可能会引入不必要的复杂性而没有任何好处。
假设您初始化MassPing
类并使用Button处理程序等待PingAll()
的结果。单击(注意处理程序是async
):
private async void btnMassPing_Click(object sender, EventArgs e)
{
btnMassPing.Enabled = false;
// Create a collection of existing Controls that have a BackgroundImage property
var controls = new Control[] { /* a list of existing Controls */ };
// The Addresses count must match the Controls'
var addresses = [An array of strings representing IpAddresses or Host names]
var massPing = new MassPing();
await massPing.PingAll(addresses, controls, 2000);
btnMassPing.Enabled = true;
}
注意:为了简单起见,PingAll()
方法自己创建了一个IProgress<T>
委托。您可能更喜欢从初始化MassPing
类的过程中传递一个委托给这个方法。
这样,您就不需要将控件集合传递给方法。
如果你在WinForms应用程序中使用这个类并不重要,如果你想将这个类移动到一个库中,它确实(或可能)很重要。
using System.Collections.Generic;
using System.Drawing;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Windows.Forms;
public class MassPing
{
private Bitmap imageRed = Properties.Resources.Red;
private Bitmap imageGreen = Properties.Resources.Green;
private Bitmap imageYellow = Properties.Resources.Yellow;
public async Task PingAll(string[] addresses, Control[] controls, uint timeout = 2000)
{
// Add more checks on the arguments
if (addresses.Length != controls.Length) {
throw new ArgumentException("Collections length mismatch");
}
var obj = new object();
var tasks = new List<Task>();
var progress = new Progress<(int sequence, object reply)>(report => {
lock (obj) {
// Use the reply Status value to set any other Control. In this case,
// it's probably better to have a UserControl that shows multiple values
var status = IPStatus.Unknown;
if (report.reply is PingReply pr) {
status = pr.Status;
Bitmap img = status is IPStatus.Success
? pr.RoundtripTime > 30 ? imageYellow : imageGreen
: imageRed;
controls[report.sequence].BackgroundImage?.Dispose();
controls[report.sequence].BackgroundImage = img;
}
else if (report.reply is SocketError socErr) {
if (socErr == SocketError.HostNotFound) {
status = IPStatus.DestinationHostUnreachable;
}
controls[report.sequence].BackgroundImage?.Dispose();
controls[report.sequence].BackgroundImage = imageRed;
}
}
});
// Add all tasks
for (int seq = 0; seq < addresses.Length; seq++) {
tasks.Add(PingAsync(addresses[seq], (int)timeout, seq, progress));
}
// Could use some exception handling
await Task.WhenAll(tasks);
}
private async Task PingAsync(string ipAddress, int timeOut, int sequence, IProgress<(int seq, object reply)> progress)
{
var buffer = new byte[32];
var ping = new Ping();
try {
var options = new PingOptions(64, true);
PingReply reply = await ping.SendPingAsync(ipAddress, timeOut, buffer, options);
progress.Report((sequence, reply));
}
catch (PingException pex) {
if (pex.InnerException is SocketException socEx) {
progress.Report((sequence, socEx.SocketErrorCode));
}
}
finally {
ping.Dispose();
}
}
}
要回答最初的问题,我认为这应该足够了:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(500);
Parallel.For(0, ipAddress.Count(), (i, loopState) =>
{
Ping ping = new Ping();
PingReply pingReply = ping.Send(ipAddress[i].ToString());
this.BeginInvoke((Action)delegate ()
{
if (pingReply.Status == IPStatus.Success)
{
if (pingReply.RoundtripTime > 50)
Image.FromFile(@"C:UsersPCDownloadsyellow.png");
else
Image.FromFile(@"C:UsersPCDownloadsgreen.png");
}
else
{
Image.FromFile(@"C:UsersPCDownloadsred.png");
}
});
});
}
但是,不要一次又一次地从磁盘加载映像,将其保存在变量中以节省磁盘访问。