I have an application where I send an instruction to a micro via the PC serial port by a button click. The micro then streams back the data which fires the data received event handler. This is captured into a string.
At this point I wish to use the string's data and populate my listview box. I can do this using invoke, delegate because I am still in the data received thread.
Is there any way I can call an event handler or simple routine to do this after the thread has exited, so I don't need to use invoke, delegate? Building the routine works ok if it's triggered by a button, but I would like it to be called programmatically to complete the task.
Hope it's clear enough, it's my first post.
Edit: Here is some sample code --
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//use 28591 or("ISO-8859-1") to cover all hex bytes from 0 - 255
serialPort1.Encoding = Encoding.GetEncoding(28591);
//wait for download to complete by monitoring cts line
if (HoldData == true)
{
while (serialPort1.CtsHolding == true) ;
HoldData = false;
}
else
Thread.Sleep(50);
string text = serialPort1.ReadExisting();
switch (text[0])
{
case '?': MemLabelUpdate(); break;
case '>': WriteConfig(text); break;
case '=': SealTest(text); break;
case '<': CurrentNumber(text); break;
default: DataDownload(text); break;
}
}
The first byte of string text is an identifier as to what has come in. This in turn calls a function which populates lables on the main form using the invoke delegate method as its running within the data received thread. The default call to the download data function passes text and sorts it out as this is a mass of events. The results are then passed to my listview box into relevant columns. I want to get away from using the invoke delegate method. I need to exit the port_datareceived thread to do this and upon exit, enter my function to just update the list as below. How can i trigger this kind of event programatically.
private void btnDisplayData_Click(object sender, EventArgs e)
{
int SectionStart = 10;
int SectionEnd = 8;
listView1.Items.Clear();
listView1.View = View.Details;
listView1.GridLines = true;
//Add columns to listview
listView1.Columns.Add("Event", 80, HorizontalAlignment.Center);
listView1.Columns.Add("Time", 80, HorizontalAlignment.Center);
listView1.Columns.Add("Date", 80, HorizontalAlignment.Center);
//Print results to listview box
ListViewItem ListItem;
for (int i = 0; i < 10; i++)
{
ListItem = listView1.Items.Add(DownloadedData.Substring(SectionStart, SectionEnd));
SectionStart += 8;
ListItem.SubItems.Add(DownloadedData.Substring(SectionStart, SectionEnd));
SectionStart += 8;
ListItem.SubItems.Add(DownloadedData.Substring(SectionStart, SectionEnd));
SectionStart += 8;
}
foreach (ColumnHeader column in listView1.Columns)
{
column.Width = -2;
}
}