c# - ping 子网时程序锁定

标签 c# .net wpf backgroundworker ping

我是编程新手。我在 C#/WPF 中创建了这个 GUI,它对用户输入的子网执行 ping 操作。它可以工作,只是当我 ping 整个 255 IP 子网时它会卡住。我还希望它在实时返回值时显示结果,但它会等到整个子网被 ping 通后才显示值。我还想要某种进度条。有人可以看一下我的代码并帮助我弄清楚我需要在这里做什么吗? C# 代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Net.Sockets;
    using System.Net.NetworkInformation;


    namespace PingTrueFalseSubnet  {

        public partial class Window1 : Window  {
            public Window1()  {
                InitializeComponent();
            }

            private void executeButton_Click(object sender, RoutedEventArgs e)  {
                string ipString;
                for (int i = 1; i < 100; i++)  {
                    ipString = inputTextBox.Text + "." + i;

                    //set the ping options, TTL 128
                    PingOptions pingOptions = new PingOptions(128, true);

                    //create a new ping instance
                    Ping ping = new Ping();

                    //32 byte buffer (create empty)
                    byte[] buffer = new byte[32];

                            try  {

                            //send the ping 4 times to the host and record the returned data.
                            //The Send() method expects 4 items:
                            //1) The IPAddress we are pinging
                            //2) The timeout value
                            //3) A buffer (our byte array)
                            //4) PingOptions

                            PingReply pingReply = ping.Send(ipString, 1000, buffer,   pingOptions);

                                //Make sure we don't have a null reply
                                if (!(pingReply == null))  {
                                    switch (pingReply.Status)  {
                                        case IPStatus.Success:
                                            outputTextBox.AppendText
                                                (string.Format("\nReply from {0}: bytes={1} time={2}ms TTL={3} Hostname={4}\n",
                                                pingReply.Address, pingReply.Buffer.Length, pingReply.RoundtripTime, pingReply.Options.Ttl, 
                                                System.Net.Dns.GetHostEntry(ipString).HostName));
                                            break;
                                        case IPStatus.TimedOut:
                                            outputTextBox.AppendText(string.Format("\n{0} Connection has timed out...\n", ipString));
                                            break;
                                        default:
                                            outputTextBox.AppendText(string.Format("\n{0} Ping failed: {1}\n",ipString, 
                                                pingReply.Status.ToString()));
                                        break;
                                    }
                                }
                                else
                                    outputTextBox.AppendText(string.Format("\n{0} Connection failed for an unknown reason...\n", ipString));
                            }
                            catch (PingException ex)  {
                                outputTextBox.AppendText(string.Format("\n{0} Connection Error: {1}\n", ipString, ex.Message));
                            }
                            catch (SocketException ex) {
                                outputTextBox.AppendText(string.Format("\n{0} Connection Error: {1}\n", ipString, ex.Message));
                            }

                    }
                }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                outputTextBox.Clear();
            }
        }
    }

下面是 XAML 标记:

    <Window x:Class="PingTrueFalseSubnet.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Ping True False Subnet" Height="376" Width="509" Name="pingMainWindow">
        <Grid>
            <TextBox Height="33" Margin="0,32,27,0" Name="inputTextBox" VerticalAlignment="Top" HorizontalAlignment="Right" 
                     Width="175"></TextBox>
            <Label Height="30" Margin="138,35,204,0" Name="inputLabel" VerticalAlignment="Top" FontSize="14" 
                   VerticalContentAlignment="Top">Enter subnet:</Label>
            <Label Height="28" Margin="60,108,0,0" Name="label1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="99">Results:</Label>
            <Button Height="27" Margin="150,77,162,0" Name="executeButton" VerticalAlignment="Top" Click="executeButton_Click">Execute</Button>
            <TextBox Margin="60,135,48,39" Name="outputTextBox" BorderBrush="Black" IsReadOnly="True" TextWrapping="Wrap" 
                     HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" />
            <Button Height="21" Margin="150,0,162,8" Name="clearButton" VerticalAlignment="Bottom" Click="button1_Click">Clear</Button>
            <ProgressBar Height="22" HorizontalAlignment="Right" Name="progressBar" VerticalAlignment="Bottom" Width="146" />
        </Grid>
    </Window>

最佳答案

您可以检查this发帖了解如何异步pnig。这只是示例,并非您的 WPF 应用程序的确切解决方案。

关于c# - ping 子网时程序锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14592499/

相关文章:

c# - 如何从 xml 中检索属性

c# - EF 迁移和时间戳列,无法更新/运行

wpf - 如何在 WPF ListView 中的项目之间添加填充?

c# - 在这种情况下如何在 asp.net 中访问数据库?

C# - 如何检查是否启用了外部防火墙?

c# - Xamarin 表单级联样式表 (CSS) 不工作。加载样式文件失败

c# - Mysql 连接器类型

c# - 使用 LINQ 将具有相同值的多行列表连接成单行

c# - WPF 数据绑定(bind)有什么意义?实现 INotifyPropertyChange 与手动 GUI 更新

wpf - MultiDataTrigger 与 AlternationIndex