This is a continuation of my previous question Here's my mainwindow.cs
public partial class MainWindow : Window
{
ObservableCollection<string> store;
public MainWindow()
{
SerialPort _serialPort = new SerialPort(SerialCom.findCOMPort(), 115200, Parity.None, 8, StopBits.One);
_serialPort.Handshake = Handshake.None;
string[] query = new string[3] { "t02", "t03", "t04" };
store = new ObservableCollection<string> { " ", " ", " " };
this.DataContext = this;
Thread thread = new Thread(delegate(){Process(store,query,_serialPort);});
thread.IsBackground = true;
try
{
thread.Start(); //catch sudden serial port closure exception
}
catch (Exception)
{
thread.Abort();
}
}
public static void Process(ObservableCollection<string> store, string[] query, SerialPort _serialPort)
{
while (true)
{
for (int i = 0; i < 3; i++)
{
string add = SerialCom.returnData(query[i], _serialPort);
if (store[i] != add)
{
store.Insert(i,add);
}
}
Thread.Sleep(300);
}
}
I have a WPF listview control in my XAML page. I would like to bind that control to the ObservableCollection being changed in the non UI thread. It seems like the fly in the ointment is the infinite loop I have to query the device while the program is running.In addition, since ObservableCollection implements INotifyPropertyChanged, when I bind the collection to the control, the control should automatically update without any other code in code behind or XAML?