C# Ping is not fast enough



我正在尝试扫描电脑是在线还是离线。但是我当前的代码以良好的性能缓慢扫描,就像计算机离线一样,有 3 到 5 秒的延迟。我什至添加了设置为 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;
    }

我也已经阅读了有关异步ping的信息,但是我找不到合适的解决方案,如果计算机在线,则仅返回true,如果计算机脱机则返回false。

提前谢谢。

如果您假装 Ping 计算机列表,您可以使用 Parallel 或使用异步Task

我用相同的 77 个 IP 测试了这两种方法。使用的变量sec = 3

    00:
  • 00:02.7146249
  • 并行 toke 00:00:05.9941404

使用这些方法

Dictionary<string, bool> pingsReturn = await Network.PingListAsync(dictionary,3);

我可以给你两个例子:

任务

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 - 我知道这是一个老问题,但仍然有效。

相关内容

  • 没有找到相关文章

最新更新