如果所有主机都无法访问,SendPingAsync会使应用进入中断模式

时间:2017-10-11 05:31:09

标签: c# wpf visual-studio ping

我正在编写我的第一个应用程序,但我遇到了一个问题。输入一个在我的foreach循环退出后我的应用程序无法访问的地址进入中断模式,但是如果foreach获得结果,它会正常进行。

这是抛出异常的代码的一部分:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    /// Verify that input box is not blank
    if (string.IsNullOrWhiteSpace(inputBox.Text))
    {
        MessageBox.Show("Cannot be left blank !", "Error");
        return;
    }

    /// Creates a list of Gateway options to try from
    string[] gatewayArray = { "cadg0", "frdg0", "dedg0", "gbdg0", "iedg0", "usdg0", "dg", "dg0" };

    /// Specifies where to get store number from
    string storeNumber = inputBox.Text;
    string pingReply;
    string pingStatus;

    clearButton_Click(sender, e);

    Ping ping = new Ping();

    foreach (string gateway in gatewayArray)
        try
        {
            /// Replace store number with "Wait" text and changes color of box to red.
            inputBox.Text = "Please Wait...";
            inputBox.Background = Brushes.Red;

            /// Pings selected store using Async method
            PingReply reply = await ping.SendPingAsync(gateway + storeNumber, 2000);

            pingReply = reply.Address.ToString();
            pingStatus = reply.Status.ToString();

                /// Displays results of Ping
                ipOne.Clear();
                ipOne.Text = pingReply;
                statusOne.Clear();
                statusOne.Text = pingStatus;
                if (statusOne.Text == "Success")
                {
                    statusOne.Text = "- ONLINE -";
                    statusOne.Background = Brushes.LightGreen;
                }
                else
                {
                    statusOne.Background = Brushes.Orange;
                }

                /// Get name of host
                IPHostEntry ipHostOne = Dns.GetHostEntry(pingReply);
                string ipOneName = ipHostOne.HostName;
                ipOneName = ipOneName.Substring(0, ipOneName.LastIndexOf(".") - 10);
                nameOne.Text = ipOneName.ToUpper();

            }

            catch (ArgumentOutOfRangeException ex)
            {
                MessageBox.Show(ex.Message);
            }

            catch (PingException)
            {
                /// Catches exceptions and continues
                /// MessageBox.Show("Unreachable !");
            }

如果我指定的商店编号不正确,我会:

  

抛出异常:' System.ArgumentOutOfRangeException'在mscorlib.dll中       未处理的类型' System.ArgumentOutOfRangeException'发生了       在mscorlib.dll中       StartIndex不能小于零。

The program '[19616] SbPinger.exe' has exited with code -1 (0xffffffff).

我尝试用try / catch处理它:

catch (ArgumentOutOfRangeException ex)
{
   MessageBox.Show(ex.Message);
}

然而,catch无效,程序仍然进入Break模式。

我的程序只是ping所有可能的默认网关,如果找到一个,则返回IP,然后ping该网络上的各个PC。除非Ping没有到达任何网关,否则其他所有工作都有效。请放轻松,因为我使用C#只用了几个星期。任何帮助将不胜感激。

彼得

1 个答案:

答案 0 :(得分:0)

因此,在查看了一段时间并尝试不同的事情之后,我发现使用Dns.GetHostEntry而不是ping URL,产生更快的结果,并且不会因为所有进程完成而崩溃。如果有人有兴趣,这是代码:

// Get IP from DG adrress
            try
            {
                IPHostEntry hostEntry = Dns.GetHostEntry("URL");
                IPAddress[] address = hostEntry.AddressList;
                textBox.Text = address.GetValue(0).ToString();
            }
            catch
            {
                // Catches Exception and moves on
                // MessageBox.Show("No result", "Error");
            }

感谢您的回复!