[英]C# Ping is not fast enough
I am trying to make a scan wether the pc is online or offline.我正在尝试扫描电脑是在线还是离线。 But my current code is way to slow to scan with a good performance as if an computer is offline there is a delay of 3 to 5 seconds.
但我目前的代码是一种扫描速度慢的方式,性能良好,就好像计算机离线时有 3 到 5 秒的延迟。 I even added the timeout parameter set as 500 but it still takes more than 3 seconds if a computer is offline.
我什至将超时参数设置为 500,但如果计算机离线,它仍然需要 3 秒以上。
public bool PingComputer(string computername)
{
bool check = false;
Ping ping = new Ping();
try
{
PingReply reply = ping.Send(computername, 500);
check = reply.Status == IPStatus.Success;
}
catch (PingException)
{
}
return check;
}
I also already read about asynchron pings but i could´nt find a suitable solution yet that simply returns true if computer is online or false if it is offline.我也已经阅读了有关异步 ping 的信息,但我找不到合适的解决方案,如果计算机在线则返回 true,如果离线则返回 false。
Thanks in advance.提前致谢。
If you pretended to Ping a list of Computers you can use Parallel
or use async Task
.如果您假装 Ping 计算机列表,您可以使用
Parallel
或使用 async Task
。
I tested both methods bellow with the same 77 IPs.我使用相同的 77 个 IP 测试了以下两种方法。 Used variable
sec = 3
.使用变量
sec = 3
。
To use the methods使用方法
Dictionary<string, bool> pingsReturn = await Network.PingListAsync(dictionary,3);
I can give you the 2 examples:我可以给你两个例子:
public static async Task<Dictionary<string, bool>> PingListAsync(Dictionary<string, bool> HostList, int sec = 3)
{
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
// set a quick TTL
PingOptions options = new PingOptions(20, true);
// internal support Task to handle Ping Exceptions like "host not found"
async Task<KeyValuePair<string, bool>> PingHost(string host)
{
try
{
var pingresult = await Task.Run(() => new Ping().SendPingAsync(host, sec * 1000, buffer, options));
//t.Wait();
if (pingresult.Status == IPStatus.Success)
return new KeyValuePair<string, bool>(host, true);
else
return new KeyValuePair<string, bool>(host, false);
}
catch
{
return new KeyValuePair<string, bool>(host, false);
}
}
//Using Tasks >>
var watch = new Stopwatch();
watch.Start();
var tasksb = HostList.Select(HostName => PingHost(HostName.Key.ToString()));
var pinglist = await Task.WhenAll(tasksb);
foreach (var pingreply in pinglist)
{
HostList[pingreply.Key] = pingreply.Value;
}
watch.Stop();
Log.Debug("PingList (Tasks) Time elapsed: " + watch.Elapsed);
//Using Tasks <<
return HostList;
}
public static async Task<Dictionary<string, bool>> PingListAsync(Dictionary<string, bool> HostList, int sec = 3)
{
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
// set a quick TTL
PingOptions options = new PingOptions(20, true);
//Using Parallel >>
watch = new Stopwatch();
watch.Start();
// avoid exception "Collection was modified; enumeration operation may not execute."
// we need new dictionary and add values
Dictionary<string, bool> dictionary = new Dictionary<string, bool>();
Parallel.ForEach(HostList.Keys, (currHost) =>
{
try
{
var pingReply = new Ping().Send(currHost, sec * 1000, buffer, options);
if (pingReply.Status == IPStatus.Success)
dictionary.Add(currHost, true);
else
dictionary.Add(currHost, false);
}
catch
{
// handle Ping Exceptions like "host not found"
dictionary.Add(currHost, false);
}
});
watch.Stop();
Log.Debug("PingList (Parallel) Time elapsed: " + watch.Elapsed);
//Using Parallel <<
return dictionary;
}
PS - I know this is an old question, but is still valid. PS - 我知道这是一个老问题,但仍然有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.