简体   繁体   中英

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#

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

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; } } ...

In main method I got it show data in 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...

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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