Keep getting cross thread error and cannot figure out a potential fix

  • Thread starter Thread starter Notoriousg13
  • Start date Start date
N

Notoriousg13

Guest
Good day guys. I am new to object oriented programming and c#, just started learning and i have a problem in my code which i cant figure out how to fix.

using System;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;


namespace CshSerial
{
public partial class Form1 : Form
{
private SerialPort _serialPort;

public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;

}
private void Form1_Load(object sender, EventArgs e)
{
foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
{
availablePorts.Items.Add(s);
}

string baud1 = "115200";
baudRateCB.Items.Add(baud1);



}
int i = 0;
private void Send(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();

}



private void Read(object sender, EventArgs e)
{


}

private void Browse_file(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Browse G-Code";
fdlg.InitialDirectory = @"";
fdlg.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
richTextBox1.Text = File.ReadAllText(fdlg.FileName);
}

}

private void availablePorts_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void baudRateCB_SelectedIndexChanged(object sender, EventArgs e)
{
string chosenPort = availablePorts.GetItemText(availablePorts.SelectedItem);
string selectedBaud = baudRateCB.GetItemText(baudRateCB.SelectedItem);
int baudint = Int32.Parse(selectedBaud);
_serialPort = new SerialPort(chosenPort, baudint, Parity.None, 8, StopBits.One);
_serialPort.Open();


}



private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{

}

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
try
{
_serialPort.Open();
}
catch (Exception ex)
{

}

if (_serialPort.ReadLine() == null)
{
Send(sender, e);
}


string[] lines = Regex.Split(richTextBox1.Text, "\n");


foreach (var line in lines)
{

textToSend.Text = lines;

}

i++;

Repeat:
_serialPort.WriteLine(textToSend.Text);
string buffer = _serialPort.ReadLine();
if (i == lines.Length && buffer.Contains("InPos"))
{
i = 0;
serialConsole.Clear();
_serialPort.DiscardInBuffer();
textToSend.Clear();
return;

}

serialConsole.Focus();
serialConsole.AppendText(Environment.NewLine + buffer);

if (buffer.Contains("InPos"))
{
do
{
_serialPort.DiscardInBuffer();

Send(sender, e);
}
while (i == lines.Length);
}
else
{

goto Repeat;
}
}
}

}

I keep getting the cross thread error on line:

string[] lines = Regex.Split(richTextBox1.Text, "\n");

I tried using backgroundworker to allow my form not to freeze when it is sending and reading data. I tried to use a new thread which starts when Send event occurs but i couldn't make it work. Could you please help me fix the error or atleast help me figure how to make the code in the

backgroundWorker1_DoWork

event execute in a separate thread so my form is not frozen.

Thank you.

Continue reading...
 
Back
Top