Hello (:
I'm trying to update the UI using ObservableCollection but can not .. I think my mistake derives from the problem of DataBinding.
The Entity class - only get & set string ststus of the ping replay.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Pinger
{
public class Entity
{
public Entity(string status)
{
_status = status;
}
private string _status;
public string Status
{
get { return _status; }
set { _status = value; }
}
}
}
PingManager class that derives from ObservableCollection
and i make Add(new Entity(status.ToString()));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Threading;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace Pinger
{
class PingManager : ObservableCollection
{
private List _ipAddressList;
private Ping pingSender;
private List _listOfPingObjects;
public PingManager(System.Collections.IEnumerable listOfIpAddress)
{
_listOfPingObjects = new List();
_ipAddressList = new List();
foreach (var ipAddress in listOfIpAddress)
{
_ipAddressList.Add(ipAddress.ToString());
}
}
internal void StartPing()
{
foreach (var pingItem in _ipAddressList)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPing), pingItem);
}
}
private void ThreadPing(object ipAddress)
{
AutoResetEvent waiter = new AutoResetEvent (false);
pingSender = new Ping();
pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
pingSender.SendAsync(ipAddress.ToString(), waiter);
_listOfPingObjects.Add(pingSender);
waiter.WaitOne();
}
private void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
PingReply replay = e.Reply;
var address = e.Reply.Address;
var status = e.Reply.Status;
var roundTripTime = e.Reply.RoundtripTime;
var ttl = e.Reply.Options.Ttl;
string allReplayInfo = status.ToString() + " " + address.ToString() + " " + roundTripTime.ToString() + " " + ttl.ToString();
Add(new Entity(status.ToString()));
}
}
}
The code behind of MainWindow
I do not know whether to write here something related to-ObservableCollection, DataBinding, DataContext????
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.Collections.ObjectModel;
using System.ComponentModel;
namespace Pinger
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private PingManager _pinger;
public MainWindow()
{
InitializeComponent();
}
private void btn_addIpAddress_Click(object sender, RoutedEventArgs e)
{
if (tb_ipAddressOct1.Text == "" || tb_ipAddressOct2.Text == "" || tb_ipAddressOct3.Text == "" || tb_ipAddressOct4.Text == "")
{
return;
}
string ipAddress = tb_ipAddressOct1.Text + "." + tb_ipAddressOct2.Text + "." + tb_ipAddressOct3.Text + "." + tb_ipAddressOct4.Text;
if (!cb_ipAddressList.Items.Contains(ipAddress))
{
cb_ipAddressList.Items.Add(ipAddress);
cb_ipAddressList.SelectedIndex = 0;
}
}
private void btn_pingIt_Click(object sender, RoutedEventArgs e)
{
_pinger = new PingManager(cb_ipAddressList.Items);
_pinger.StartPing();
}
}
}
I don't know if the Window.Resources is ok or not...?
i just need to update ListBox when i get replay status of ping.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Pinger"
Title="Pinger" Height="350" Width="525">
Anyway, the idea is that when there is an update to the Status property in the ListBox be able to get this update and display it.
Help me if you could demonstrate the solution on my code (:
Thank you so much for helping!!!
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaganathan BantheswaranPosted Jan 8, 2014, 11:30 PM
Try to init PingManager as like below,
public MainWindow()
{
InitializeComponent();
DataContext = new PingManager();
}