简体   繁体   English

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

[英]Create a PING Monitor in WPF Application - With DataGrid

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; ...公共类ServerMonitor {公共字符串ID {设置; get; 得到; } public string Timeout { set; } public string Timeout {set; get; 得到; } public string IP { set; }公用字符串IP {set; get; 得到; } public string TTL { set; }公用字符串TTL {set; get; 得到; } } ... }} ...

In main method I got it show data in datagridview. 在主要方法中,我得到它在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! 谢谢,对不起!

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. 要在DataGrid中显示具有在UI中自动更新的值的项目列表,您需要将DataGrid绑定到实现INotifyPropertyChanged接口的对象的ObservableCollection。

The code which performs the actual pinging would reside in your ViewModel which will also contain the ObservableCollection. 执行实际ping操作的代码将驻留在您的ViewModel中,该模型还将包含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. 该代码将定期遍历该ObservableCollection中的对象,并使用ping结果更新每个对象的所需public属性,并且这些值将在DataGrid中自动更新,因为它们实现了INotifyPropertyChanged接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM