Hello. So I am making an application that sets up a serial port and creates a DataReceived event whenever there is data received on the serial port. Now, I want to update a listbox in my UI whenever the data received event occurs but a cross threading error occurs. I have no exposure to threads and the article on MSDN about invoke is too sparse. So I am attaching my code and can you please edit it to include invokes such that my listbox gets refreshed whenever there is a DataReceived event. Thanks!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Collections;
using System.Management;
using System.Threading;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace testListBox
{
/*Class1 is used to automatically find and open a serial port, and has an event handler for whenever there is DataReceived*/
class Class1
{
/*just defining variables to automatically allocate a serial port, no need to follow*/
public SerialPort ZigbeePort;
public string[] available_ports = SerialPort.GetPortNames();
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("HARDWARE\\DEVICEMAP\\SERIALCOMM", false);
public string[] valueNames;
string name_of_module = "VCP0";
public int number_ports;
public string COM_port_name;
List<byte> HandlerReceived = new List<byte>();
public Form1 form;
/*just opening a COM port, do not need to follow*/
public void init()
{
valueNames = myKey.GetValueNames(); /*Store the value names of the keys in a string array*/
number_ports = valueNames.Length;
for (int i = 0; i < number_ports; i++)
{
if (valueNames[i].EndsWith(name_of_module))
{
ZigbeePort = new SerialPort(available_ports[i], 9600, Parity.None, 8, StopBits.Two);
if (!ZigbeePort.IsOpen)
{
ZigbeePort.Open();
}
COM_port_name = available_ports[i];
}
}
ZigbeePort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
/*Data Received handler*/
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
byte[] TempRxData = new Byte[ZigbeePort.BytesToRead];
ZigbeePort.Read(TempRxData, 0, TempRxData.Length);
for (int i = 0; i < TempRxData.Length; i++)
{
HandlerReceived.Add(Convert.ToByte(TempRxData[i]));
}
/*Just adding a name to the items list that the Listbox has*/
form.a.Add("Handler Item");
System.Console.WriteLine(HandlerReceived.Count);
}
}
public partial class Form1 : Form
{
public BindingList <string> a = new BindingList <string>();
Class1 clas = new Class1();
public Form1()
{
InitializeComponent();
listBox1.DataSource = null;
a.Add("1st Item");
a.Add("2nd Item");
listBox1.DataSource = a;
clas.init();
clas.form = this;
a.ListChanged += new ListChangedEventHandler(a_ListChanged);
}
private void button1_Click(object sender, EventArgs e)
{
a.Add("Button item");
}
void a_ListChanged(object sender, ListChangedEventArgs e)
{
/*cross threading occurs here*/
listBox1.Refresh();
}
}
}