在WPF应用程序中创建PING监视器 - 使用DataGrid

时间:2022-05-31 00:32:33

I'm new in the forum. Sorry for my English... It's not very well.

我是论坛的新手。对不起我的英文...这不是很好。

Please, I read the article: How to Perform Multiple "Pings" in Parallel using C#

请阅读文章:如何使用C#并行执行多个“Ping”

Please, someone give me a help and to explain how can I populate a WPF Datagrid to monitoring a list of IP's ?

请有人给我一个帮助,并解释如何填充WPF Datagrid来监控IP列表?

I'm build a class:

我正在建立一个班级:

... public class ServerMonitor { public string ID { set; get; } public string Timeout { set; get; } public string IP { set; get; } public string TTL { set; get; } } ...

... public class ServerMonitor {public string ID {set;得到; public string Timeout {set;得到;公共字符串IP {set;得到;公共字符串TTL {set;得到; } ...

In main method I got it show data in datagridview.

在main方法中,我得到它在datagridview中显示数据。

        MonitorPing.PingAddressesAsync(new List<IPAddress>() {
            IPAddress.Parse ("XXX.XXX.XXX.XXX"),
            IPAddress.Parse ("XXX.XXX.XXX.XXX"),
            IPAddress.Parse ("XXX.XXX.XXX.XXX"),
            IPAddress.Parse ("XXX.XXX.XXX.XXX")
        }, delegate (Task<List<PingReply>> tpr)
        {
            var lr = tpr.Result;

            foreach (var pr in lr)
            {
                //This correct
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,(ThreadStart)(() => dataGrid.Items.Add(new ServerMonitor { ID = pr.Buffer.ToString(), Timeout = pr.Status.ToString(), IP = pr.Address.ToString(), TTL = pr.RoundtripTime.ToString() })));
            }
        });
    }

At this point, it's all right.

在这一点上,它没事。

But how do I get the data to be updated in real time? I use a while, use a System.Timer...

但是如何实时更新数据呢?我用一段时间,使用System.Timer ...

Tks and sorry for anything!

Tks,对不起任何事!

1 个解决方案

#1


0  

To display a list of items in the DataGrid with values that are automatically updated in the UI you will need to bind your DataGrid to an ObservableCollection of objects which implement INotifyPropertyChanged interface.

要使用在UI中自动更新的值显示DataGrid中的项列表,您需要将DataGrid绑定到实现INotifyPropertyChanged接口的ObservableCollection对象。

The code which performs the actual pinging would reside in your ViewModel which will also contain the ObservableCollection. That code would periodically iterate through the objects in that ObservableCollection and update the desired public Properties of each object with ping results and these values will be automatically updated in the DataGrid because they implement the INotifyPropertyChanged interface.

执行实际ping的代码将驻留在ViewModel中,ViewModel也将包含ObservableCollection。该代码将定期迭代ObservableCollection中的对象,并使用ping结果更新每个对象的所需公共属性,这些值将在DataGrid中自动更新,因为它们实现了INotifyPropertyChanged接口。

#1


0  

To display a list of items in the DataGrid with values that are automatically updated in the UI you will need to bind your DataGrid to an ObservableCollection of objects which implement INotifyPropertyChanged interface.

要使用在UI中自动更新的值显示DataGrid中的项列表,您需要将DataGrid绑定到实现INotifyPropertyChanged接口的ObservableCollection对象。

The code which performs the actual pinging would reside in your ViewModel which will also contain the ObservableCollection. That code would periodically iterate through the objects in that ObservableCollection and update the desired public Properties of each object with ping results and these values will be automatically updated in the DataGrid because they implement the INotifyPropertyChanged interface.

执行实际ping的代码将驻留在ViewModel中,ViewModel也将包含ObservableCollection。该代码将定期迭代ObservableCollection中的对象,并使用ping结果更新每个对象的所需公共属性,这些值将在DataGrid中自动更新,因为它们实现了INotifyPropertyChanged接口。